javascript - How to replace a letter with another letter in a textbox -
say want target specific letter in textbox region , replace letter using either jquery or javascript, how , still able target letter afterwards, , again, , again etc.? letter change , changed inputed either textbox or dropdown (whichever easier)
p.s tried jquery's replace function , didn't work (i didn't use correctly though :)
$( "button" ).click( function() { $( "changed" ).replacewith( $( "new" ) ); });
$("button").click(function() { var old = $("#changed").val(); var replacement = $("#new").val(); var regexp = new regexp(old, 'g'); $("#input").val(function(i, current) { return current.replace(regexp, replacement); }); });
- you need use
#
before ids select them. - you need values of inputs using
.val()
. - jquery's
.replacewith
replacing entire dom elements, not changing value of input. - you use
.val()
change value of input. when argument function, function gets called current value parameter, , return value put in place. - the javascript method
.replace()
used perform replacements in strings. multiple replacements, need useregexp
g
flag.
Comments
Post a Comment