How to select an HTML element that has "data-name"?
How to select HTML element that has "data-name"?
29831-Jul-2023
Updated on 01-Aug-2023
Home / DeveloperSection / Forums / How to select HTML element that has "data-name"?
How to select an HTML element that has "data-name"?
Aryan Kumar
01-Aug-2023There are a few ways to select an HTML element that has a data-name attribute. Here are a few methods:
Using CSS
You can use the
data-name
attribute as a selector in CSS. For example, the following CSS will select all elements with the data-name attribute of "my-name":CSS
Using JavaScript
You can also use JavaScript to select elements with the data-name attribute. The following JavaScript code will select all elements with the data-name attribute of "my-name":
JavaScript
The
elements
variable will contain an array of all the elements with the data-name attribute of "my-name".Using the jQuery library
You can also use the jQuery library to select elements with the data-name attribute. The following jQuery code will select all elements with the data-name attribute of "my-name":
JavaScript
The
elements
variable will contain an array of all the elements with the data-name attribute of "my-name".Here is an example of how to use this code:
HTML
This code will create two div elements, one with the data-name attribute of "my-name" and one with the data-name attribute of "other-name". The
each()
method will loop through the elements and print the element to the console.