Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select item by data-id in pure javascript

Tags:

javascript

I have an applescript program that executes javascript in Google Chrome's front browser tab.

Whenever my script is run, I need to select the element with data-id play-pause and .click it. I cannot use jQuery as it's not included with the document.

Is there a way in pure javascript that I can do this?

(I'm sure there's probably a way to import it so I could use it but I'm not going down that path for a simple program.)

like image 998
Spotlight Avatar asked Jul 05 '26 18:07

Spotlight


1 Answers

From IE8 and up you can use querySelector, or querySelectorAll if there's more than one element

document.querySelector('[data-id="play-pause"]').click();
like image 92
adeneo Avatar answered Jul 08 '26 07:07

adeneo