3

How get an array of the values of elements which have same class?

When I do this I get only the first element, but I want a whole array:

var classes = document.querySelector(".klass").value;
alert(classes); //Outputs only the first element

And I want to get a full array of the values of the inputs:

<input type="text" class="klass" />
<input type="text" class="klass" />
<input type="text" class="klass" />

Is that possible?

7
  • var elements = document.getElementsByClassName("klass"); ? Commented Dec 31, 2014 at 11:31
  • 5
    ... or use document.querySelectorAll() instead. Commented Dec 31, 2014 at 11:32
  • Seriously guys why the vote down querySelector is a method to use css selectors with JavaScript ? Commented Dec 31, 2014 at 11:35
  • 1
    Apart from a typo in the post, just reading the documentation would have made this clear to you ; ). Commented Dec 31, 2014 at 11:37
  • 2
    Downvoting is up to the person, but it is not unreasonable to downvote a question that show a notable lack of research and/or care (such as the spelling of querySelector). SO is not a crowd-sourced API documentation service. Commented Dec 31, 2014 at 11:42

4 Answers 4

7

Use document.querySelectorAll to retrieve a NodeList (see also the section "How can I convert NodeList to Array?") then cast it to an array and map a function that returns each element's value.

var classesNodeList = document.querySelectorAll(".klass");
var classes = Array.prototype.slice.call(classesNodeList).map(function(element) {
    return element.value;
});

Update


As stated in the comment by Ginden, a shorter way to do this is to pass the NodeList to Array.prototype.map using Function.prototype.call

var classesNodeList = document.querySelectorAll(".klass");
var classes = Array.prototype.map.call(classesNodeList, function(element) {
    return element.value;
});
Sign up to request clarification or add additional context in comments.

5 Comments

You can even use [].map.call(nodeList, func). ;)
Yus it's shorter ineed :)
Can you please simplify how does the function return the value in which form an array or each one in independent variable most of this classes and method are new for me
@Maroxtn I added links the mdn documentation of each functions and features described above it would be too long to describe it here.
Object.setPrototypeOf(NodeList.prototype, Array.prototype); or NodeList.prototype.__proto__ = Array.prototype; to have all array methods
3

For such a simple CSS selector expression, I would use getElementsByClassName and give it the class name, rather than querySelectorAll. getElementsByClassName is generally faster than using querySelectorAll by several orders of magnitude. See this jsperf.

var classes = document.getElementsByClassName("klass"); // Do not use a period here!
var values = Array.prototype.map.call(classes, function(el) {
    return el.value;
});

getElementsByClassName is usually faster than querySelectorAll. Browsers index elements by class name already to optimize the speed of CSS transformations. getElementsByClassName returns an HTMLCollection element, so it does not have the Array methods and you need to use Array.prototype... on this too.

Comments

2

You need to loop through your array of elements and get the value of each one.

var classes = document.querySelectorAll(".klass").value,
    values = [];

for(var i = 0; i < classes.length; i++) {
    values.push(classes[i].value);
}

Note that this may not be as clean as using [].map, but is a good deal faster.

2 Comments

Why do you push classes array into another one or its just a sanity check that everything is fine ?
You could write over the same array (i.e. classes[i] = classes[i].value), but I prefer to avoid doing that because of potential issues/inconsistencies. It will give you essentially the same result, though.
0

You can use querySelectorAll method first and then use array's map function to retrieve the result:

var elements = document.querySelectorAll('.klass');
var values = [].map.call(elements, function(e) {
  return e.value;
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.