Jquery.inside ()?

I was wondering if it is possible to insert content inside html tags. Say you have an h4 tag:

<h4>Some Header</h4>

      

And you want the text to be surrounded by a span with a "string" class as well. For instance:

<h4><span class="line">Some Header</span></h4>

      

I know about things like .after (), but is there something like .inside () for this? \

Ok ... I know I can do it manually, but in this situation it is out of the question.

thanks

+2


a source to share


5 answers


This should work:



$('h4').contents().wrapAll('<span class="line" />');

      

+2


a source


$('h4').wrapInner('<span class="line" />');



+3


a source


Yes, it's called $ .wrapInner ()

http://api.jquery.com/wrap/

<h4>Some Header</h4>

      

and then:

$('h4').wrapInner('<span class="line" />');

      

+2


a source


You should try wrapInner .

The .wrapInner () function can take any string or object that can be passed to the $ () factory function to indicate the DOM structure. This structure can be nested several levels deep, but should contain only one most closed element. the structure will be wrapped around the content of each of the elements into a set of consistent elements.

+1


a source


If you want to find existing content and wrap it (to <h4>

have the content wrapped <span>

as shown) try wrap and children .

0


a source







All Articles