javascript replace div tags for p tags where it has no class

Posted by Tom on Stack Overflow See other posts from Stack Overflow or by Tom
Published on 2012-10-04T15:30:06Z Indexed on 2012/10/04 15:37 UTC
Read the original article Hit count: 336

Filed under:
|

I would like to replace div tags for p tags but only when the div tag does not have a class.

So this:

<div class="myDiv">
   <div>sdfglkhj sdfgkhl sdfkhgl sdf</div>
   <div>dsf osdfghjksdfg hsdfg</div>
</div>

Would become:

<div class="myDiv">
   <p>sdfglkhj sdfgkhl sdfkhgl sdf</p>
   <p>dsf osdfghjksdfg hsdfg</p>
</div>

I've tried .replace("<div>", "<p>").replace("</div>","</p>") but this replaces the closing tag of the one with a class.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about replace