Letfar
Letfar

Reputation: 3503

How to NOT select div's which have empty title or have not? (JQuery)

I've tryed construction like this:

$('div[title!=""]')

but div's with no title are also in.

Upvotes: 1

Views: 418

Answers (2)

Flash Thunder
Flash Thunder

Reputation: 12036

$('div[title]').css('background','green'); // if has title attr
$('div[title=""]').css('background','blue'); // if has empty title or has no title attr
$('div:not([title])').css('background','red'); // if has no title attr
$('div[title]:not([title=""])').css('background','yellow'); // if has title but not empty

CHECK LIVE

PS. Last one in example overwrites the first one (changes the background from green to yellow). Just try to comment out all lines except one, and see what happens. Second line overwrites one from first aswell.

Upvotes: 0

potashin
potashin

Reputation: 44581

Empty title :

$('div[title=""]')

Not empty title with :not selector:

$('div:not([title=""])')

Not empty title with not() method:

 $('div').not('[title=""]')

JSFiddle

Upvotes: 1

Related Questions