JZ.
JZ.

Reputation: 21877

How do you eliminate an all divs of a child class but one?

I have the following DOM structure:

<div class="city">
  <div class="a">Delete</div>
  <div class="b">Selected</div>
  <div class="c">Delete</div>
</div>

How do you save city and div class b, but delete div class a and c if you know that the user has selected "b" (you know this using logs your console.log(variable))

Basically what JQuery method would you use to do this and how?

Upvotes: 2

Views: 74

Answers (6)

Josiah Ruddell
Josiah Ruddell

Reputation: 29831

$('.city').find('.a, .c').remove();

Or

$('.city div:not(.b)').remove();

With class variable:

var selectedClass = 'b';
$('.city div').not('.' + selectedClass).remove();

Upvotes: 1

Matthew Nessworthy
Matthew Nessworthy

Reputation: 1428

$('.city > div:not(.b)').remove();

Upvotes: 2

Sampson
Sampson

Reputation: 268354

Given your structure, I'd probably use the following:

$(".b").siblings().remove();

See it in action: http://jsbin.com/akuno3/edit

Upvotes: 2

luca
luca

Reputation: 37136

Basically I would do:

 $(".b").click(function () { 
  $(".a,.c").remove(); 
}); 

Upvotes: 0

Chandu
Chandu

Reputation: 82913

Try this:

$("div.city div.b").click(function() {
    $(this).siblings().remove()
})

Upvotes: 0

Dustin Laine
Dustin Laine

Reputation: 38503

$('.city').children('div').not('.b').remove();

Upvotes: 3

Related Questions