How to replace part of a string with an html tag?

How to replace part of a string with html tag in jQuery?
Say, for example, <div>Who am i</div> should be <div><b>Who</b> am i</div> .

+4
source share
2 answers

You can use the html callback function and the replace method.

 $('div').html(function(_, oldHTML){ return oldHTML.replace(/(\w+)/, '<b>$1</b>'); // return oldHTML.replace('Who', '<b>Who</b>'); }) 

http://jsfiddle.net/3rAMp/

+4
source

use .html() for jquery

 $("div").html("<b>Who</b> am i") 
+1
source

All Articles