Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Javascript access data attribute page improved #37805

Closed
wants to merge 6 commits into from
Original file line number Diff line number Diff line change
@@ -28,8 +28,16 @@ Reading the values of these attributes out in [JavaScript](/en-US/docs/Web/JavaS

To get a `data` attribute through the `dataset` object, get the property by the part of the attribute name after `data-` (note that dashes are converted to {{Glossary("camel_case", "camel case")}}).


StephenMuya marked this conversation as resolved.
Show resolved Hide resolved
```html
<ul id="something">
<li data-id="1">A
<li data-id="2">B
<li data-id="3">C
StephenMuya marked this conversation as resolved.
Show resolved Hide resolved
</ul>
```
```js
StephenMuya marked this conversation as resolved.
Show resolved Hide resolved
const article = document.querySelector("#electric-cars");
const article = document.querySelector('#something [data-id="2"]')
StephenMuya marked this conversation as resolved.
Show resolved Hide resolved
// The following would also work:
// const article = document.getElementById("electric-cars")