aurora
aurora

Reputation: 24

How can I get the href value from counted links with jQuery

HTML Source Code:

<a href="http://asd.com/123/qwe">1</a>
<a href="http://asd.net/asd">2</a>
<a href="http://123.com">3</a>
<a href="http://www.a1s2d3.com">4</a>
<a href="http://www.q1w2e3.com">5</a>
<a href="http://bnm.org/questions/">6</a><br /><br />

<span id="element"></span>

jQuery Code

var total = $('a').size();
$('#element').text(total);

I get the size of a tags. But I cant get the href value of all a tags. How can I write the the loop?

Upvotes: 0

Views: 33

Answers (1)

Amit Joki
Amit Joki

Reputation: 59252

You just need this:

 $('#element').text(function(){
   return $('a').map(function(){ return this.href }).get().join(","); 
 });

Basically

var arrOfLinks = $('a').map(function(){ return this.href }).get();

will give you array of links.

Upvotes: 1

Related Questions