Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - get a list of values of an attribute from elements of a class

I have a class .object which has an attribute called level. I want to get a list of all the different values of level on the page so I can select the highest one.

If I do something like:

$(".object").attr("level") 

... will that get me a list of values that are the values of the level attribute? I suspect not, but then how do you do something like that?

Note: I don't want to select an HTML object for manipulation as is more common, rather I want to select values of the attribute.

EDIT: In order to get the highest "level" I have done this, but it doesn't seem to work. I will try the other suggested method now.

var highLevel=0; $.each(".object[level]", function(i, value) {    if (value>highLevel) {        highLevel=value;    } });  alert(highLevel); 
like image 847
Ankur Avatar asked May 02 '10 15:05

Ankur


People also ask

How get multiple data attribute values in jQuery?

that's because when you call the data method on the jQuery object, it returns the data for the first element in the nodeList. To get the data for all the div's you have to loop through the elements with the $. each method or use a mapping to retrieve the attributes in a list.

How get data attribute value of class in jQuery?

$('. myclass') will select all the elements having the class myclass , but when used . data() on it will return the data-attribute value of the first element in the matched set, thus returning 2 .

How do you find the value of an array in a class?

The GetValue() method of array class in C# gets the value at the specified position in the one-dimensional Array. The index is specified as a 32-bit integer. We have set the array values first using the Array. CreateInstance method.

What is ATTR jQuery?

jQuery attr() Method The attr() method sets or returns attributes and values of the selected elements. When this method is used to return the attribute value, it returns the value of the FIRST matched element.


1 Answers

$(".object").attr("level") will just return the attribute of first the first .object element.

This will get you an array of all levels:

var list = $(".object").map(function(){return $(this).attr("level");}).get(); 
like image 130
Kobi Avatar answered Sep 16 '22 18:09

Kobi