tpow
tpow

Reputation: 7884

Getting an Id value using a class value using jQuery

I have several divs that I want to work with dynamically.

<div class="parent-19" id="123">  </div>
<div class="parent-19" id="124">  </div>
<div class="parent-19" id="125">  </div>

Now, I want to iterate through them like:

 $('.parent-19').each(function () {
           //Want to load the id into a variable
    });

How can I load the id value (e.g. 123) into a variable?

var Id = $(this).???

Upvotes: 0

Views: 1006

Answers (1)

David Tang
David Tang

Reputation: 93664

You don't need to wrap this with the jQuery object to get the id. Simply do:

var id = this.id;

To get an array of ids:

var ids = $('.parent-19').map(function () {
    return this.id;
}).get();

Upvotes: 3

Related Questions