user3676578
user3676578

Reputation: 223

How to get all the different values from elements that are members of the same class in jquery?

<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

Answers (3)

Alex Char
Alex Char

Reputation: 33218

You can use something like that:

var array_v  = [];

$(".abc").each(function(i, e){
    array_v[i] = $(this).attr("value");
});

fiddle

Upvotes: 5

Sudharsan S
Sudharsan S

Reputation: 15393

Use .map in jquery

var res = $("a.abc").map(function(val, i) {

        return $(this).val();

}).get();

Upvotes: 0

Samuli Hakoniemi
Samuli Hakoniemi

Reputation: 19049

var array_v = [].map.call(document.getElementsByClassName('abc'), function(elem) {
  return elem.getAttribute("value");
});

Upvotes: 2

Related Questions