Skip to content Skip to sidebar Skip to footer

How To Check If Div Has Id Or Not?

one
two

Solution 1:

Use attribute selectorselector[attribute] to get only the elements that have an ID

$('.myClass[id]')   // Get .myClass elements that have ID attribute

In your case:

$('.ui-droppable[id]').each(function(){
   alert( this.id );
});

jsFiddle demo

Solution 2:

if(this.id) is all you need.


Why will this work?

If the element has an ID, the value will a non-empty string, which always evaluates to true. If it does not have an ID, the value is an empty string which evaluates to false.


I am trying to get ids from the loop which are there.

To get a list of IDs, you can use .map like so:

var ids = $(".ui-droppable").map(function() {     
    returnthis.id ? this.id : null;
}).get();

or use the selector Roko suggests in his answer.

Solution 3:

demohttp://jsfiddle.net/QRv6d/13/

APi link: http://api.jquery.com/prop/

Please try this, this should help

code

   $(".ui-droppable").each(function () { 

       if($(this).prop("id").length > 0)
       {
       alert('here');
       }
    });​

Solution 4:

If there is no id attribute attr method will return undefined. Just write:

if($(this).attr("id"))

Solution 5:

if(typeof $(this).attr("id") != 'undefined')

Post a Comment for "How To Check If Div Has Id Or Not?"