Ad

Select Item By Data-id In Pure Javascript

- 1 answer

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.)

Ad

Answer

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

document.querySelector('[data-id="play-pause"]').click();
Ad
source: stackoverflow.com
Ad