55

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.

Aaron Brewer
  • 3,567
  • 18
  • 48
  • 78
  • 3
    Use `.is('[id]');` to check if a specific element has an `id` attribute at all. Or if you need to filter a collection of elements, use `.filter('[id]')` and then compare the `.length` property – Ian Jun 24 '13 at 14:24
  • Why do you want to do this? What are you actually planning to do? – musefan Jun 24 '13 at 14:25
  • 1
    possible duplicate of [jquery get only all html elements with ids](http://stackoverflow.com/questions/1163888/jquery-get-only-all-html-elements-with-ids) – MrCode Jun 24 '13 at 14:25
  • jQuery documentation: [Has Attribute Selector \[name\]](http://api.jquery.com/has-attribute-selector/). – Felix Kling Jun 24 '13 at 14:25
  • Do you want to "select elements" to do something with/to them, or test if any such elements exist? – nnnnnn Jun 24 '13 at 14:29
  • I just want to do be able to fire off a script if there are elements within the parent class that have the id attribute in the element. – Aaron Brewer Jun 24 '13 at 14:33
  • Thank you all for the help, suggestions, and improvements! :) – Aaron Brewer Jun 24 '13 at 14:43

11 Answers11

63

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

A. Wolff
  • 74,033
  • 9
  • 94
  • 155
50

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.

Community
  • 1
  • 1
  • 3
    No. That will tell you if an element has a *specific* id. The question is asking if it has any id. See the answer from two years ago that was accepted. – Quentin May 26 '15 at 13:17
  • 3
    This is a great answer however if you want to check if the element has a specific id! +1 – The Codesee Jul 02 '17 at 11:19
7

You can do

document.getElementById(id) or 
$(id).length > 0
Ani
  • 4,473
  • 4
  • 26
  • 31
  • 3
    No, the OP is trying to determine which elements have an id attribute defined with any value, not select by a specific, known id. – nnnnnn Jun 24 '13 at 14:31
6

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: how to check if div has id or not?

Community
  • 1
  • 1
brian_wang
  • 401
  • 4
  • 16
  • 1
    But the question says "elements" (plural), and `.attr()` will only get the id from the first element matching that selector. – nnnnnn Jun 24 '13 at 14:33
5

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

$('.parent a[id]').length
Pierre de LESPINAY
  • 44,700
  • 57
  • 210
  • 307
5

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)
}
ArunValaven
  • 1,753
  • 2
  • 25
  • 44
4

I seemed to have been able to solve it with:

if( $('your-selector-here').attr('id') === undefined){
    console.log( 'has no ID' )
}
JSTL
  • 808
  • 1
  • 14
  • 25
justMoritz
  • 83
  • 6
3

Pure js approach:

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

JsFiddle Demo

Wallstrider
  • 856
  • 1
  • 7
  • 22
1

Simply use:

$(".parent a[id]");
Broxzier
  • 2,909
  • 17
  • 36
1

You can do this:

if ($(".parent a[Id]").length > 0) {

    /* then do something here */

}
Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42
Ian Dev
  • 11
  • 3
  • Welcome to Stack Overflow! This answer was already contributed multiple times. Please check other answers first. – snwflk Apr 12 '19 at 20:57
0

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();
        }
    });
});
TGEE
  • 119
  • 9