Thursday, 15 April 2010

jquery - Wrap a new div element around specific text -



jquery - Wrap a new div element around specific text -

i struggling one.

how can wrap new element around text not have class or id?

below scenario:

<div class="contentdiv">.....</div> share knowledge. <a href="url">be first write review »</a>

i need new div wrap around "share knowledge. <a href="url">be first write review »</a>"

i tried this:

$(document).ready(function(){ $('.contentdiv').each(function(){ $(this).add( $(this).next() ).wrapall('<div class="newdiv"></div>'); }) })

but not working because wraps around <a> element , leaves text below it. need rest of text in there well.

please help!

thank in advance.

~ elena

you need next textnode after .contentdiv , wrap that:

$('.contentdiv').each(function() { $(this).next('a').add(this.nextsibling).wrapall('<div class="newdiv"></div>'); });

fiddle

since jquery does'nt textnodes, native nextsibling should work.

jquery html text wrap

No comments:

Post a Comment