-3

I know how to access HTML elements using:

document.getElementById(element_ID);

but what if there's no ID attributes?

Example:

<ul data-vehicle="Car">
    <li data-model="Sedan">2.5HP</li>
</ul>
Sengkek
  • 1
  • 2
  • 1
    [Document.querySelector()](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector) + [Select elements by attribute in CSS](https://stackoverflow.com/questions/5324415/select-elements-by-attribute-in-css) – DBS Sep 07 '22 at 08:36
  • I agree with @DBS – debugger Sep 07 '22 at 08:39
  • Does this answer your question? [Select elements by attribute in CSS](https://stackoverflow.com/questions/5324415/select-elements-by-attribute-in-css) – debugger Sep 07 '22 at 08:44
  • there should be a `''` while referring to an Id `document.getElementById('element_ID');` who has edited the question? – debugger Sep 07 '22 at 08:46

1 Answers1

2

You can use document.querySelector("[data-model='Sedan']") but keep in mind that querySelector is not supported on IE7.

user19902261
  • 121
  • 5