Select element by data attribute in JavaScript


Select element by data attribute in JavaScript : We sometimes need to select element by data attribute in javascript. You can select the element simply using the document.querySelectorAll() in javascript. Here in this tutorial we are going to explain how you can select the element using the data attribute in javascript with example and demo.


Select element by data attribute in JavaScript

You can select the attribute using the data attribute as below –

javascript get element by data attribute value Example:

<a my-id='122'>19293049503</a>
<script type="text/javascript">
var elements = document.querySelectorAll('[my-id]');
var d = elements[0].innerHTML;
alert(d);
</script>

Try it »

If you run the above example it will show you the 19293049503. The output will look something like this-

Select element by data attribute in JavaScript: example


Advertisements

Add Comment

📖 Read More