HTML & CSS · Junior · Question 7 of 75

How do you read the value of the data-user-id="42" attribute in JavaScript?

Explanation

All data-* attributes are available through element.dataset, with kebab-case names converted to camelCase: data-user-id → dataset.userId. The value is always a string. You can also use getAttribute('data-user-id'). In CSS they are accessed via the [data-user-id] selector and the attr() function.

Further reading

Problem with a question

HTML & CSS · Junior · question 7

Test yourself

Take the HTML & CSS · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz