0

I there a way to wrap text inside this div into <p></p>?

<div class="remove">
<h2 class="MsgHead">Messages</h2>
<img height="16" width="16" src="img.jpg"/>
Error saving details
<img height="16" width="16" src="img.jpg" alt="information"/>
Please check your entries  and try again
</div>

I meant those sentences that are outside any tags (inside remove)

2
  • Please clarify. Could you provide "before" and "after" examples? Commented Oct 5, 2009 at 14:24
  • Note that <p><div>...</div></p> is not valid HTML since block tags are not allowed inside a <p>. Commented Oct 5, 2009 at 14:26

3 Answers 3

3

Assuming you are hoping to end up with

<div class="remove">
  <h2 class="MsgHead">Messages</h2>
  <img height="16" width="16" src="img.jpg"/>
  <p>Error saving details</p>
  <img height="16" width="16" src="img.jpg" alt="information"/>
  <p>Please check your entries  and try again</p>
</div>

and borrowing some code from How do I select text nodes with jQuery?

I came up with this. (which I haven't tested, but, even if it doesn't work, hopefully you get the general idea.)

$('div.remove')
  .contents()
  .filter(function() {
    return this.nodeType == Node.TEXT_NODE;
  }).wrap('<p/>');
Sign up to request clarification or add additional context in comments.

1 Comment

You might want to filter out the whitespace-only text nodes: return (this.nodeType==Node.TEXT_NODE)&&this.wholeText.replace(/[ \t\n]/g,'')
0
$('<p></p>').html($('div.remove').html())

Comments

0

Not really with the markup you have currently, I mean theoretically you can, but the markup you seem to want to wrap consists of header elements and you can't place block elements (which header is) inside paragraph elements. (I assumed you wanted the <p> tag inside the <div>, if you actually want to wrap the <div> itself in <p> then that will actually throw an error in IE).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.