jQuery if Element has an ID?

JavascriptJquery

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

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

Solution 2 - Javascript

You can use jQuery's .is() function.

> if ( $(".parent a").is("#idSelector") ) { > > //Do stuff > > }

It will return true if the parent anchor has #idSelector id.

Solution 3 - Javascript

You can do

document.getElementById(id) or 
$(id).length > 0

Solution 4 - Javascript

Number of .parent a elements that have an id attribute:

$('.parent a[id]').length

Solution 5 - Javascript

You can using the following code:

   if($(".parent a").attr('id')){

      //do something
   }


   $(".parent a").each(function(i,e){
       if($(e).attr('id')){
          //do something and check
          //if you want to break the each
          //return false;
       }
   });

The same question is you can find here: https://stackoverflow.com/questions/11137317/how-to-check-if-div-has-id-or-not

Solution 6 - Javascript

Simple way:

Fox example this is your html,

<div class='classname' id='your_id_name'>
</div>

Jquery code:

if($('.classname').prop('id')=='your_id_name')
{
    //works your_id_name exist (true part)
}
else
{ 
    //works your_id_name not exist (false part)
}

Solution 7 - Javascript

Pure js approach:

var elem = document.getElementsByClassName('parent');
alert(elem[0].hasAttribute('id'));

JsFiddle Demo

Solution 8 - Javascript

I seemed to have been able to solve it with:

if( $('your-selector-here').attr('id') === undefined){
    console.log( 'has no ID' )
}

Solution 9 - Javascript

Simply use:

$(".parent a[id]");

Solution 10 - Javascript

You can use each() function to evalute all a tags and bind click to that specific element you clicked on. Then throw some logic with an if statement.

See fiddle here.

$('a').each(function() {
    $(this).click(function() {
        var el= $(this).attr('id');
        if (el === 'notme') {
            // do nothing or something else
        } else {
            $('p').toggle();
        }
    });
});

Solution 11 - Javascript

You can do this:

if ($(".parent a[Id]").length > 0) {
    
    /* then do something here */

}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAaron BrewerView Question on Stackoverflow
Solution 1 - JavascriptA. WolffView Answer on Stackoverflow
Solution 2 - Javascriptviren KalkhudiyaView Answer on Stackoverflow
Solution 3 - JavascriptAniView Answer on Stackoverflow
Solution 4 - JavascriptPierre de LESPINAYView Answer on Stackoverflow
Solution 5 - Javascriptbrian_wangView Answer on Stackoverflow
Solution 6 - JavascriptArunValavenView Answer on Stackoverflow
Solution 7 - JavascriptWallstriderView Answer on Stackoverflow
Solution 8 - JavascriptjustMoritzView Answer on Stackoverflow
Solution 9 - JavascriptBroxzierView Answer on Stackoverflow
Solution 10 - JavascriptTGEEView Answer on Stackoverflow
Solution 11 - JavascriptIan DevView Answer on Stackoverflow