My selected Link My Link 1

Finding wrapper text using html

I have the following HTML code:

<div id="Test">
  My selected Link
  <a href="link1.html">My Link 1</a>
  <a href="link2.html">My Link 2</a>
</div>

      

I need to find a text string inside

<div id="Test"></div>

and wrap it with a tag. It will always only have one text line inside

<div id="Test"></div>

,

but the text will have different places in the code, for example:

<div id="Test">
  <a href="link1.html">My Link 1</a>
    Another Selected Link
  <a href="link2.html">My Link 2</a>
</div>

      

The result I'm looking for looks something like this:

<div id="Test">
  <a href="link1.html">My Link 1</a>
   <h2>Another Selected Link</h2>
  <a href="link2.html">My Link 2</a>
</div>

      

So to summarize: find a single text line inside the div (but not in the children tags) and wrap it with a tag.

Many thanks!

+3


source to share


1 answer


Hope this helps in your specific case

jsBin demo



$('#Test').contents(':not(a)').wrap('<h2/>');

      

http://api.jquery.com/not-selector/
http://api.jquery.com/contents/
http://api.jquery.com/wrap/

0


source







All Articles