Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove all elements except one in jQuery?

I'm using jQuery 1.12. After clicking on a TD, I want to remove all elements within it except the one with class "savedBlock", so I tried

$(elt).closest('td').find('.savedBlock').show()
$(elt).closest('td').not('.savedBlock').remove() 

Unfortunately this is having the effect of removing everything . At least everything disappears from the table cell after I run this. If I comment out the $(elt).closest('td').not('.savedBlock').remove() line, nothing is removed but now I see more than what I want. Any suggestions?


1 Answers

Try this one:

$('td').on('click', function(e) {
    $(this).children().not(".savedBlock").remove();
})
like image 63
Monzurul Shimul Avatar answered Nov 17 '25 07:11

Monzurul Shimul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!