Daniel
Daniel

Reputation: 4342

replace class name with javascript

I need help finding an element with a specific class name and then replace it with a new class name. Here is the code I came up with. I get no results. The class name does not change, and there are no errors in the console.

Updated Code:

var classMatches = document.querySelectorAll(".crux.attachFlash");
for (var i = 0; i < classMatches.length; i++) {
    classMatches[i].className = " ";
}

Upvotes: 0

Views: 608

Answers (1)

David Thomas
David Thomas

Reputation: 253318

Because you need to amend the class-name of the matched element, not the array of elements:

var classMatches = document.querySelectorAll(".crux.attachFlash");
for (var i = 0; i < classMatches.length; i++) {
    classMatches[i].className = " ";
}

You forgot the [i] index, so you were trying to set the className for whole array (Which, as you've found, does not work).

Upvotes: 3

Related Questions