0

I need to add a class to bulk of <a> tag. I have to select the a tag using querySelectorAll method, and all elements are selected. But when I trying to add a class to them it won't added.

I have try the following example.

var qc=document.querySelectorAll(".Label ul li a");
qc.className+="newcls";

https://jsfiddle.net/ahhvovvv/

But I can add it through jQuery, but don't use jQuery.
The working jQuery code is

$(document).ready(function(){

   $(".Label ul li a").addClass("newcls");

});

3
  • 2
    qc is a collection. You need to iterate over it and add the classname to each collection element. jQuery does that automatically for you. Commented Apr 9, 2016 at 20:32
  • 1
    Also, qc[i].classList.add("newcls") might work better for you. See: MDN classList Commented Apr 9, 2016 at 20:37
  • 2
    [].forEach.call(qc, el => el.classList.add('newCls')); Commented Apr 9, 2016 at 20:39

3 Answers 3

3

I'd suggest:

// converts the collection of elements returned from
// document.querySelectorAll() into an Array, using
// Array.from(), then iterates over that Array using
// Array.prototype.forEach():
Array.from( document.querySelectorAll(".Label ul li a") ).forEach(

  // using arrow function syntax to perform the same
  // action on each <a> ('aElement') within the
  // array; here using Element.classList API to
  // add the given class-name:
  aElement => aElement.classList.add('newClass')
);
Sign up to request clarification or add additional context in comments.

2 Comments

In DOM4, NodeLists are iterable, so you can use forEach directly without Array.from. See forEach method of Node.childNodes. But there is not much browser support yet.
up vote with reference: MDN Array.from.
2

You have to iterate through the Collection, one way could be:

var qc = document.querySelectorAll(".Label ul li a");
[].forEach.call(qc, function(item) {
    item.className+=" newcls";
})

Fiddle updated

Remember to add a space before the class if you'll use className, since you are editing the whole class, otherwise you could use

item.classList.add('newcls');

2 Comments

up vote as I think using [].forEach.call is a safer solution than using Array.from.
It's an easy solution, however there are some highlighted problems by Todd Motto, interesting to keep in mind when work in a big codebase. However I think is also a good practice to use classList instead of className.
1

In your code qc is collection. You should iterate through it. And className is not a collection it is a string where class names separated with space. So here is your code fixed

var qc=document.querySelectorAll(".Label ul li a");
for(var i=0; i<qc.length; i++) {
    qc.classList.add("newcls");
    //replace with next line if you need this working in IE before version 10 
    //qc.item(i).className+=" newcls";
}

2 Comments

Why is using “for…in” with array iteration such a bad idea? NodeLists may have enumerable properties like item, which you don't want to iterate.
Yep, it is NodeList, but not in array. There's nothing criminal in for .. in, and by the best is to use for .. of

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.