Sure, here is how to select an element by dataattribute with jQuery:
JavaScript
// Select the element with the data-name attribute of "my-name"
var element = $("[data-name='my-name']");
The element variable will contain a reference to the element with the data-name attribute of "my-name".
Here is an example of how to use this code:
HTML
<html>
<body>
<div data-name="my-name">This is a div with the data-name attribute of "my-name"</div>
<div data-name="other-name">This is a div with the data-name attribute of "other-name"</div>
<script>
// Select the element with the data-name attribute of "my-name"
var element = $("[data-name='my-name']");
// Do something with the element
console.log(element);
</script>
</body>
</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
console.log() method will print the element with the data-name attribute of "my-name" to the console.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Sure, here is how to select an element by data attribute with jQuery:
JavaScript
The
elementvariable will contain a reference to the element 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
console.log()method will print the element with the data-name attribute of "my-name" to the console.