views:

124

answers:

1

I need to find all the p tags inside all the divs with a class of someClass and wrap them with another div. This is how the beginning mark up would look like :

<div class="someClass">
   // Lots of different tags generated by the site
   <p>Some text</p>
   <p>Some text</p>
   <p>Some text</p>
   <p>Some text</p>
</div>

<div class="someClass">
   // Lots of different tags generated by the site
   <p>Some text</p>
   <p>Some text</p>
</div>

Would turn into :

<div class="someClass">
   // Lots of different tags generated by the site
   <div class="bla">
   <p>Some text</p>
   <p>Some text</p>
   <p>Some text</p>
   <p>Some text</p>
   </div>
</div>

<div class="someClass">
   // Lots of different tags generated by the site
   <div class="bla">
   <p>Some text</p>
   <p>Some text</p>
   </div>
</div>

Any ideas? When I try using .each() : for each div with a class of someClass wrap all the p tags, but it just wraps them all together in the top div. I've tried many other things without success. thanks for having a look!

+1  A: 

Have you tried this?

$('div.someClass p').wrapAll(...);

Or this?

$('div.someClass').each(function() {
  $(this).find('p').wrapAll(...);
});

Edit

After looking at the code you posted, it appears to be a syntax issue. You need quotes in this line:

$(this).find('p').wrapAll(<div class='toggle'></div>);

It should be:

$(this).find('p').wrapAll("<div class='toggle'></div>");
John Fisher
I've tried that and similar things. It's really giving me a headache now!! I've posted an example of the problem on :http://pastebin.me/30acb260acc142dd0c9979aca9812390
Turbodurso
@John - your code *almost* works, just need to change the `<div>` class to `someClass`. Here's a Working Demo - http://jsbin.com/oxexe add **/edit** to the URL to see the code
Russ Cam
@Russ: I'm not sure what you mean. The edited section is a modification of a line within his code, where he is inserting the "toggle" div. It shouldn't be changed to "someclass".
John Fisher
@John - check out the case of the CSS class of the `<div>` in your selector compared to the CSS class of the `<div>` already in the HTML page in the question - you have `someclass` whereas the question has `someClass`. According to the HTML 4.01 spec, class and names are case-sensitive (although some browsers are not compliant with the spec) - http://devedge-temp.mozilla.org/viewsource/2001/css-class-id/
Russ Cam
Ah. I hadn't tested it or intended it to be copy-paste worthy. Thanks for pointing that out, though.
John Fisher