Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access element via dataset attribute directly

Tags:

javascript

Assuming I have following mark up:

<div id="Movie" data-genre="horror">The Hills Have Eyes</div>

I know that I can access that element´s dataset attributes using:

document.getElementById("Movie").dataset.genre

or

document.getElementById("Movie").getAttribute("data-genre")

But is there a way to get all elements with the same genre without using any other feature? I am thinking about something like that:

document.getElementsByDataSetKey(key)
like image 968
Amberlamps Avatar asked Nov 19 '12 08:11

Amberlamps


1 Answers

You could try querySelectorAll

document.querySelectorAll('[data-genre~="horror"]');

will give you all elements with data genre that contains horror.

like image 130
Musa Avatar answered Oct 14 '22 16:10

Musa