4

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?

stark
  • 2,246
  • 2
  • 23
  • 35
  • So the savedBlock is being applied to the td? – Zze Feb 16 '17 at 21:21
  • Can you show your HTML? It's hard to know host all the elements are related. `.not()` applies to the element itself, `.find()` looks for descendants. – Barmar Feb 16 '17 at 21:30

3 Answers3

4

Try this one:

$('td').on('click', function(e) {
    $(this).children().not(".savedBlock").remove();
})
Monzurul Shimul
  • 8,132
  • 2
  • 28
  • 42
0
$(elt).closest('td:not(.savedBlock)').remove() 
Amir H. Bagheri
  • 1,416
  • 1
  • 9
  • 17
0
$(elt).closest('td:not(.savedBlock)').remove() 

is how it should be done. the colon is before the not

azizj
  • 3,428
  • 2
  • 25
  • 33