Reputation: 68650
var tab1 = $('.tabs a:first-child').attr('href');
alert(tab1);
.. matches only one although there are two div.switch
on the same page. The second is down on the page, not in the same parent.
Edit: Basically my question is, :first-child should find all the elements, but it just stops after one like :first
Upvotes: 1
Views: 173
Reputation: 14318
Try selecting one object at one time for getting attribute. .tabs a:first-child
will select all .tabs
and its first a
element.
var tab1 = $('.tabs:first-child a:first-child').attr('href');
alert(tab1);
var tab2 = $('.tabs:last-child a:first-child').attr('href');
alert(tab2);
Delphist solution should also work.
Upvotes: 1
Reputation: 4539
$('.tabs').each(function() {
alert($('a:first-child', $(this)).attr('href'));
});
Upvotes: 3