Finnur
Finnur

Reputation: 135

Remove a div with the same class as another div

I have two divs:

<div class="dialog large"></div>

and

<div class="dialog"></div>

I have to remove the one with the class "dialog" but keep the one with "dialog large".

If I do $('dialog').remove(); they are both removed.

Can anyone help me with this?

Upvotes: 2

Views: 483

Answers (4)

woemler
woemler

Reputation: 7169

if you want to remove DIVs where the class is exactly "dialog", try:

$('div[class="dialog"]').remove();

Upvotes: 0

thecodeparadox
thecodeparadox

Reputation: 87073

$('div.dialog:not(.large)').remove();

DEMO

A little explain

div.dialog will select div with class=dialog (in this case both div will select). But

div.dialog:not(.large) will exclude those div with class large and remove them.

Related Refs

Upvotes: 4

elclanrs
elclanrs

Reputation: 94101

There are many many ways to do it. You could also use filter() as an alternative maybe suitable for other more complicated cases.

$('div.dialog').filter(function(){ return !$(this).is('.large') })

Upvotes: 0

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167172

Use this:

$("div.dialog").not('.large').remove();

Fiddle here...

Upvotes: 0

Related Questions