You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The dataset property in JavaScript allows you to access all data attributes of an HTML element as a DOMStringMap object. It simplifies retrieving, modifying, or interacting with custom data stored in attributes like data-id or data-name.
830
+
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
831
+
<h4>Syntax</h4>
832
+
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
833
+
<pre><code>
834
+
const e = document.getElementByID('demo') // Accessing the element
835
+
const dataId = e.dataset.dataID //Access the data-id attribute
The getAttribute() method in JavaScript retrieves the value of a specified attribute from an HTML element. To get data attributes, use element.getAttribute(‘data-attribute’). This method allows precise selection and manipulation of individual data attributes without accessing all at once.
When working with the DOM in JavaScript, selecting elements by their class names is a common task. JavaScript provides several methods to achieve this, whether we need to select one or multiple elements. In this article, we will cover different approaches to get elements by class name in JavaScript.
960
+
961
+
<h4>Prerequisites</h4>
962
+
- HTML
963
+
- CSS
964
+
- JavaScript
965
+
966
+
Below are the following approaches to get elements by class name in Javascript:
967
+
968
+
<h4>Table of Content</h4>
969
+
970
+
- Using document.getElementsByClassName()
971
+
- Using document.querySelector()
972
+
- Using document.querySelectorAll()
973
+
974
+
<h3>1. Using document.getElementsByClassName()</h3>
975
+
In this approach we are using the document.getElementsByClassName() method. This method selects all elements with a specific class name and returns a live HTMLCollection. Think of it as a way to collect all elements with same label. In this, list gets updated automatically if elements are added or removed.
976
+
977
+
<h4>Syntax:</h4>
978
+
<pre><code>var elements = document.getElementsByClassName("className");</code></pre>
979
+
980
+
Example: In this example we are using the getElementsByClassName() method to get element by class name in javascript.
In this approach we are using the document.querySelector() method. This method returns the first element that matches the specified selector (class name). It is useful when you only need to select a single element by class name.
1040
+
1041
+
Syntax:
1042
+
1043
+
<pre>var element = document.querySelector(".className");</pre>
1044
+
1045
+
Example: In this example we are using the querySelector() method to get element by class name and we will change the background color of selected class in javascript.
In this approach we are using the document.querySelectorAll() method. This method finds all elements that match a specific CSS selector, like class name. It gives you a static list, which means it wont automatically update if page changes.
827
1100
1101
+
Syntax:
1102
+
1103
+
```
1104
+
var elements = document.querySelectorAll(".className");
1105
+
```
1106
+
1107
+
Example: In below example we are using the querySelectorAll and we will print all the content which have that class name.
0 commit comments