Steven
Steven

Reputation: 19425

How do I get specific element in jQuery.each loop

I need to get the div containing the street address within the list. The div has a class called address ( div class="address" )

I cannot use jQuery("#storeList li .address"), because there are other elements I need to acces as well.

I have the following code:

jQuery("#storeList li").each(function() {
  var n = jQuery(this.address).text(); // <- This does not work
  alert(n);
});

How do I access each DIV element of type Address?

Upvotes: 8

Views: 29165

Answers (3)

Raghav
Raghav

Reputation: 9630

$('#storeList li').each(function() 
{
  var n = $(this).find('div.address').html(); 
  alert(n);
});

Upvotes: 4

Bill Bell
Bill Bell

Reputation: 21643

jQuery("#storeList li:has(.address) .address").each(function() {
    alert(this.innerHTML);
});

An alternative that avoids using a second query. As a jQuery newbie I don't know what the tradeoffs really are though.

Upvotes: -1

Scharrels
Scharrels

Reputation: 3055

jQuery("#storeList li").each(function() {
  var n = jQuery(this).find(".address").text(); // <- This works
  alert(n);
});

Upvotes: 16

Related Questions