tarnfeld
tarnfeld

Reputation: 26566

jQuery: Foreach element href?

The title sounds confusing.. but im sure this is possible!

I have one button, (id='downloadAll') and many <a href="..." class="link"></a> on my page. What i want to do, for the purpose of finding out how to do this.. is to do something logic like this.

var hrefs = ALL OF THE ELEMENTS WITH CLASS 'link';
foreach(hrefs){
 alert(this.attr('href'));
}

Basically for each of the elements with a class, i want to get the value of each one... in tern. Yes, i do have jquery on the page - am hoping to make use of this!

Thanks!

Upvotes: 2

Views: 6198

Answers (2)

Igor
Igor

Reputation: 1

You should write something like this:

$.each(hrefs, function(){
  alert(this.href);
});

Upvotes: 0

cletus
cletus

Reputation: 625307

$("a.link").each(function() {
  alert($(this).attr("href"));
});

Upvotes: 14

Related Questions