.find replacing content inside html tags
Posted
by
Woz
on Stack Overflow
See other posts from Stack Overflow
or by Woz
Published on 2012-10-19T10:25:24Z
Indexed on
2012/10/19
11:02 UTC
Read the original article
Hit count: 142
jQuery
I want to sanitize html tags from my wysiwyg. I want to replace certain tags with another. This almost works only the replaceWith() is also removing the content from within the tags. I dont want to do this. I just want to replace the tags. Here is what I have so far.
Text for Testing:
This<div>is</div><div>a test</div><div>to clean </div><div>some tags</div><div><br></div>
Expected Result:
This<p>is</p><p>a test</p><p>to clean</p><p>some tags</p><p><br></p>
Actual Result:
This<p></p><p></p><p></p><p></p><p></p>
This is the code I am using to find and replace
var thebad = ["h1","h2","h3","h4","h5","h6","div"];
var thegood = ["","","","","","","<p>"];
for(var i = 0; i < thebad.length; i++){
$('.content').contents().find('body').find(thebad[i]).replaceWith(thegood[i]);
}
I need to figure out how to keep the text inside the html tags when i replace them.
Thanks in advance
© Stack Overflow or respective owner