0

Possible Duplicate:
How do I add a DOM element with jQuery?

var re = new RegExp("\\b(" + l + ")\\b")
var sOpen = "<span class='highlight'>";
var sClose = "</span>";
var newhtml = sp.replace(re, sOpen + l + sClose, "gi");
alert(newhtml);
$('.highlight').css('color', 'yellow');

I am getting newHtml value as

I love to work with <span class='highlight'>jquery</span>

I am highlight the highlight class item. jquery but its not hightlighting the text. please can any body tell me is that something I am doing wrong here?

how to create a newhtml as DOm element?

thanks

Community
  • 1
  • 1
user957178
  • 631
  • 4
  • 15
  • 27

2 Answers2

2

Even better would be to wrap it with the built-in logic:

$('myelement').wrap('<span/>').parent().addClass('highlight');
AlienWebguy
  • 76,997
  • 17
  • 122
  • 145
1

You can create an object of it and add it to the DOM:

var newhtml = $(sp.replace(re, sOpen + l + sClose, "gi"));
$(body).append(newhtml); 
Bas Slagter
  • 9,831
  • 7
  • 47
  • 78
  • Thanks Baszz, so if I need to highlight the element jquery what do I need to do? thanks – user957178 Oct 10 '11 at 16:38
  • You can just refer to the element through the newhtml variable (newhtml.addClass()) for example. You can also add the id attribute to it and refer to the element that way ($('myid').addClass() for example) – Bas Slagter Oct 10 '11 at 17:14