Home > Web Front-end > JS Tutorial > How Can I Efficiently Get and Manipulate Elements by Class Name in Vanilla JavaScript?

How Can I Efficiently Get and Manipulate Elements by Class Name in Vanilla JavaScript?

DDD
Release: 2024-12-15 20:24:16
Original
625 people have browsed it

How Can I Efficiently Get and Manipulate Elements by Class Name in Vanilla JavaScript?

Get Elements by Class in JavaScript: A Comprehensive Solution

The absence of an inbuilt get elements by class function in JavaScript poses a challenge for developers who need to manipulate elements based on class attributes. In this article, we will explore an effective method for retrieving elements by class using vanilla JavaScript.

A Tale of Two Approaches: IDs vs. Classes

Traditionally, elements have been accessed via unique IDs using the getElementById() function. However, when dealing with multiple elements, IDs become impractical. Classes, on the other hand, allow for flexible grouping of elements.

Introducing the Solution

To overcome this limitation, we present a comprehensive function called replaceContentInContainer(). This function takes two parameters: a class name and the replacement content. Here's how it operates:

  • It utilizes document.getElementsByTagName('*') to retrieve all elements within the document.
  • It iterates over these elements, examining their class list for a match with the specified class name.
  • Upon finding a matching element, it replaces its innerHTML with the provided content.

Code Implementation:

function replaceContentInContainer(matchClass, content) {
    var elems = document.getElementsByTagName('*'), i;
    for (i in elems) {
        if((' ' + elems[i].className + ' ').indexOf(' ' + matchClass + ' ')
                > -1) {
            elems[i].innerHTML = content;
        }
    }
}
Copy after login

Example Usage:

replaceContentInContainer('box', 'This is the replacement text');
Copy after login

This revised solution enables efficient and flexible replacement of content within elements based on their class attributes.

Additional Notes:

  • This function is compatible with a wide range of browsers.
  • It iterates over all elements in the document, which can be computationally expensive in cases with large numbers of elements.
  • Optimization techniques, such as caching retrieved elements, can be employed to enhance performance.

The above is the detailed content of How Can I Efficiently Get and Manipulate Elements by Class Name in Vanilla JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template