Aaron Brewer Aaron Brewer -5 years ago 417
Javascript Question

jQuery if Element has an ID?

How would I select elements that have any ID? For example:

if ($(".parent a").hasId()) {
/* then do something here */

I, by no means, am a master at jQuery.

Answer Source

Like this:

var $aWithId = $('.parent a[id]');

Following OP's comment, test it like this:

if($aWithId.length) //or without using variable: if ($('.parent a[id]').length)

Will return all anchor tags inside elements with class parent which have an attribute ID specified

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download