views:

50

answers:

3

I have the following HTML:

<div class="house">...</div>

But in my code I dynamically insert in DIV ID's to make the code then look like this:

<div class="house" id="id_1">...</div>

Question: How can I get the DIV ID by only knowing the CLASS using JQuery? I've tried something like the following but it doesn't work.

$('.house').getID();
+2  A: 

Use the jQuery.attr() method to get and set attributes.

var houseId = $('.house').attr('id');

Note: This will only get the last '.house' element in the DOM's id.

munch
A: 

I believe that

$('.house').attr("id");

Should work. I didn't test it though.

Quotidian
+9  A: 
$('div.house')
  .each(function(index) {
    alert( 'id for this div.class #'+index+': '+$(this).attr('id') );
});
davidosomething
+1: it's good to note that there may be a number of different IDs based on the selector.
eyelidlessness