Reputation: 223
<a href="www.google.com" value="1000" class="abc">Link1</a>
<a href="www.google.com" value="1001" class="abc">Link2</a>
<a href="www.google.com" value="1002" class="abc">Link3</a>
<a href="www.google.com" value="1003" class="abc">Link4</a>
<a href="www.google.com" value="1004" class="abc">Link5</a>
<a href="www.google.com" value="1005" class="abc">Link6</a>
<a href="www.google.com" value="1006" class="abc">Link7</a>
<a href="www.google.com" value="1007" class="abc">Link8</a>
I am having 7 link on the UI.
i want all the links values in an array like
array_v = [1000,1001,1002,1003,1004,1005,1006,1007]
where class is same for all the links.....
Is there any way that we can get all values of same css class by jquery or javascript...
i tried with
document.getElementsByClassName('abc');
but i am getting output like this :
HTMLCollection[a.abc #, a.abc #, a.abc #, a.abc #, a.abc #, a.abc #, a.abc #]
Upvotes: 0
Views: 54
Reputation: 33218
You can use something like that:
var array_v = [];
$(".abc").each(function(i, e){
array_v[i] = $(this).attr("value");
});
Upvotes: 5
Reputation: 15393
Use .map
in jquery
var res = $("a.abc").map(function(val, i) {
return $(this).val();
}).get();
Upvotes: 0
Reputation: 19049
var array_v = [].map.call(document.getElementsByClassName('abc'), function(elem) {
return elem.getAttribute("value");
});
Upvotes: 2