tags:

views:

44

answers:

5
+2  Q: 

jquery .inside() ?

I was wondering if it was possible to insert content inside html tags. So say you have a h4 tag:

<h4>Some Header</h4>

And you want the text to also be surrounded by a span with a class of "line". Like so:

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

I know about stuff like .after() but is there something like .inside() to do this with? \

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

Thanks

+3  A: 

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

x1a4
+2  A: 

Yes, it's called $.wrapInner()

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

<h4>Some Header</h4>

and then:

$('h4').wrapInner('<span class="line" />');
arnorhs
+2  A: 

This should work:

$('h4').contents().wrapAll('<span class="line" />');
noah
thanks for the SUPER quick answer. This worked. +1 for you
codedude
A: 

If you want to find the existing content and wrap it (so that the <h4> has content wrapped with a <span> as shown), try wrap and children.

justkt
+1  A: 

You should try wrapInner.

The .wrapInner() function can take any string or object that could be passed to the $() factory function to specify a DOM structure. This structure may be nested several levels deep, but should contain only one inmost element. The structure will be wrapped around the content of each of the elements in the set of matched elements.

Soufiane Hassou