tags:

views:

61

answers:

3

What is jQuery has id equivalent of the following statement?

$('#mydiv').hasClass('foo')

so you could give a class name and check that it contains a supplied id.

something like:

$('.mydiv').hasId('foo')
+1  A: 

I would probably use $('.mydiv').is('#foo'); That said if you know the Id whay wouldnt you just apply it to the selector in the first place?

prodigitalson
A: 
$('#' + theMysteryId + '.someClass').each(function() { /* do stuff */ });
Pointy
the each is unneeded because there will never be more than one `#theMysteryId`.
prodigitalson
Well it saves you the trouble of assigning it to a variable, checking to see if the length is non-empty, and then proceeding with the code. If the selector matches nothing jQuery just won't call the "each" function, so it's nice and clean. Now if all you want to do is call some jQuery API, then sure.
Pointy
+5  A: 

If you only want to perform items to a class that has a particular ID, you could build that distinction into your selector:

$("#foo.bar");       // matches all id="foo" class="bar" elements.
$(".bar[id='foo']"); // matches all id="foo" class="bar" elements.
Jonathan Sampson