2

Possible Duplicate:
Replace all instances of a pattern with regular expressions in Javascript / jQuery
How can I use jQuery to style /parts/ of all instances of a specific word?

Say I have the code:

<div class="replace">
     <i>Blah</i>
     <u>Blah</u>
</div>

Now, I want to replace all the < within the div with something else.

If I use $('#div').html().replace('<','somethingElse'); it only replaces the first instance.

How can I replace all the instances?

Thanks

1
  • you need to use a little regex, Kevin B is refering to it I think correctly, /g is ur regex command to repeat after first find Commented Mar 14, 2012 at 19:20

1 Answer 1

9

Use regex

"anyString".replace(/</g,'somethingElse');

If you want to replace it inside the div then try this.

$('#div').html($('#div').html().replace(/</g,'somethingElse'));
Sign up to request clarification or add additional context in comments.

2 Comments

How I do if I want to do this for whole html? $('#html').html($('#html').replace(/name/g, 'somethingElse')); does'nt work.
@Phiber Did you ever learn how? If so, let me know, please.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.