Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select all elements with a class in JS

I'd like to modify all classes with JS. Is there way to select them without manually setting the array index (for example [0] or [1] or [184])?

Example code:

<div class='message'>something:</div>
<div class='message'>something</div>
const element = document.querySelectorAll(".message");
element.classList.add("color");

It works only when I add [0] and only for the first element that has the class. But I'd like to modify all the elements with the class.

like image 427
Oliver Avatar asked Mar 23 '19 15:03

Oliver


People also ask

How do you select all elements in a class?

To select elements with a specific class, write a period (.) character, followed by the name of the class. You can also specify that only specific HTML elements should be affected by a class. To do this, start with the element name, then write the period (.)

How can I get all of the elements with the same class name in JavaScript?

The JavaScript getElementsByClassName is used to get all the elements that belong to a particular class. When the JavaScript get element by class name method is called on the document object, it searches the complete document, including the root nodes, and returns an array containing all the elements.

How do you target all classes in JavaScript?

To select a specific HTML class using JavaScript, we need to target it and then store it as a variable. Here is the one line of JavaScript we need to target this element and store it as a variable: Code from a text editor: const vanillaDescription = document. querySelector('.

How do you select all elements with the same class in react?

To find all elements by className in React: Use the getElementsByClassName method to get all elements with a specific class.


3 Answers

It's important to learn what basic language syntax does first. The [0] is selecting the 0 index of an array (or array-like object). So to operate on them all, you can use a loop with a variable that is incremented starting at 0 and continuing until it goes out of bounds of the array.

function replaceEmotes() {
    var messages = document.querySelectorAll(".message");
    for (var i = 0; i < messages.length; i++) {
        var str = messages[i].innerHTML.replace(":smile:", "<i class='em em-smile'></i>");
        messages[i].innerHTML = str;
    }
}

There are other ways too, but this is a fundamental syntax that should probably be learned first.

like image 159
ziggy wiggy Avatar answered Nov 11 '22 04:11

ziggy wiggy


Use forEach to iterate over all message nodes and replace emotes. Also note that I'm using a global regexp to replace all :smile: strings within a message, not just the first one.

function replaceEmotes() {
    var messages = document.querySelectorAll(".message");

    messages.forEach(message => {
       message.innerHTML = message.innerHTML.replace(/:smile:/g, "<i class='em em-smile'></i>");
    });
}
like image 43
abadalyan Avatar answered Nov 11 '22 03:11

abadalyan


document.querySelectorAll() selects all elements of given class name and store them in an array-like object. It is possible to loop through the objects instead of accessing them manually.

var elements = document.querySelectorAll('.className');
for(var i = 0; i < elements.length; i++){
    var str = elements[i].innerHTML;
    elements[i].innerHTML = str.replace(":smile:", "<i class='em em-smile'></i>");
}

You can also do the same thing using document.getElementsByClassName()

var elements = document.getElementsByClassName('.className');
for(var i = 0; i < elements.length; i++){
    // Same like above...
}
like image 45
Adnan Toky Avatar answered Nov 11 '22 04:11

Adnan Toky