18

I have a div element with a data attribute that I need to use to select that element but I'm drawing a blank on how to do that.

HTML

<div class="element" data-id="123456789"></div>

JS

var element = document.body.querySelector('.element[data-id=123456789]');

2 Answers 2

27

You just need to add "" on data-id value

var element = document.body.querySelector('.element[data-id="123456789"]')
Sign up to request clarification or add additional context in comments.

Comments

3

Add quotes to the attribute selector value:

var element = document.body.querySelector('.element[data-id="123456789"]');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.