Skip to content
Advertisement

Javascript Click on Element by Class

So I am writing a script that can be run on a page but I want to click on this element, unfortunately, it does not have an id to get and I am trying to use the .click() function on it, but it doesn’t work, here’s what I have, anyone know how to fix it? This is the only element in the class also

JavaScript

Advertisement

Answer

I’d suggest:

JavaScript

The above code assumes that the given element has both of those classes; otherwise, if the space is meant to imply an ancestor-descendant relationship:

JavaScript

The method getElementsByClassName() takes a single class-name (rather than document.querySelector()/document.querySelectorAll(), which take a CSS selector), and you passed two (presumably class-names) to the method.

References:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement