views:

44

answers:

3

I would like to use jquery to remove some tags inside mainclass, so that this -

<div class='mainclass'>
<div class='inclass'>
<a href='#'>Some text</a>
</div>
</div>

Becomes this -

<div class='mainclass'>
Some text
</div>

Thanks in advance.

A: 

Store the text you want to preserve, remove the elements and set the text to the main div.

var sometext = $('.mainclass > .inclass > a').text();
$('.mainclass').remove('.inclass').text(sometext);
Shiki
.remove won't save the text within the inclass div.
x1a4
A: 
$('.mainclass').html($('.inclass > a').text());
ryanulit
**WRONG WRONG WRONG!** That's an XSS hole. (What if the text is `<script>...</script>`?)
SLaks
Depends how it's being used I guess. I don't follow how the text could end up as script tags though.
ryanulit
Like this: `<a>Evil: <script>alert('XSS!');</script></a>`
SLaks
+3  A: 

$('.mainclass').text($('.inclass').text());

x1a4
Nice. I didn't know .text() would ignore the html tags. Makes sense though.
ryanulit