Question

I have a working code that replaces multiple strings within a textarea. However it only replaces strings that are written onLoad in my textarea.

I want to paste some text into the textarea and replace it afterwards. What I'm doing wrong?

see: http://jsfiddle.net/yXm5P/

Here's my HTML:

<input type="button" class="change" value="change all">

<textarea class="input">Schwarz -- doesn't work for text, that was pasted into textarea, onLoad text only....why???</textarea>

Here's my script:

$(".change").click(function() {
var dictionary= {
"Schwarz":"#000000",
"Braun":"#6F3E18",
"Beige":"#D4BE8D",
"Grau":"#838383",
"Weiß":"#FFFFFF",
"Dunkelblau":"#0000A0",
"Blau":"#345AFF",
"Hellblau":"#32CAEB",
"Türkis":"#22A2A4",
"Grün":"#25B53A",
"Gelb":"#FFFC00",
"Orange":"#FF6501",
"Rot":"#EA0001",
"Pink":"#ED008C",
"Hell Lila":"#9349AA",
"Dunkel Lila":"#663376",
"Mehrfarbig":"#mcol"   
};

$("body *").each( function(){
    for( var ptrn in dictionary){
        $(this).text( $(this).text().replace(new RegExp(ptrn ,"g"), dictionary[ptrn] ) );
    }
});
      alert( "done" );


});
Était-ce utile?

La solution

.text() is NOT the correct way to get/set a textarea's value. .val() is.

Use the right function and the right result should happen ;)

Autres conseils

Here you have the right version of your jsFiddle with the .val() selector: JSfiddle replace text

$(this).val( $(this).val().replace(
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top