Question

I have the html tag as,

<div data-id='test'></div>

I need to get this element using

document.querySelectorAll('[data-id="test"]');

But the above code returns always empty array. Please help me in getting the element using querySelectorAll().

Was it helpful?

Solution

There shouldn't be any double quotes with your ID

document.querySelectorAll('[data-id=test]');

OR

document.querySelectorAll('div[data-id=test]');

And make sure you place this inside onload or make the JavaScript tag as defer="defer"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top