1

I have the following html code , and I want to search the Welcome and make it bold .

<div class="welcome-msg">Welcome, First Name | </div>

the Output should be like this using DOM manipulation.

<div class="welcome-msg"><strong>Welcome</strong>, First Name | </div>

thanks ..

1
  • may we know why you want to do that, there may be better ways e.g. if you have control over html why not put welcome in a span Commented Jun 21, 2010 at 8:29

3 Answers 3

1
var $welcomeMsg = $('.welcome.msg');
$welcomeMsg.html($welcomeMsg.text().replace(/(welcome)/i, '<strong>$1</strong>'));
Sign up to request clarification or add additional context in comments.

Comments

0

You need simple replace there:

var txt = $('div.welcome-msg').text();
$('div.welcome-msg').text(txt.replace('Welcome', '<strong>Welcome</strong>'));

Comments

0

You could load the text as a variable but doing

var myvar=$('.welcome-msg').html();

to change the text

myvar=myvar.replace('Welcome', '<strong>Welcome</strong>');

then to put it back

$('.welcome-msg').html(myvar);

or in a single line

$('.welcome-msg').html($('.welcome-msg').html().replace('Welcome', '<strong>Welcome</strong>'));

Tested and work fine

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.