Javascript: Automatic button click?

I am learning how to write a chrome extension, and I'm pretty new to javascript.

Here are some html:

<div class="button data" style=""> <a class="button1 whiteColor" href="http://link1.com">VIEW This</a> <a class="button2 redColor" href="http://link2.com">VIEW That</a> </div> 

What I want to do is open link2.com by clicking button2 using javascript.

I use the following, but it does not work: /

 document.getElementByClassName("button2 redColor").click(); 

Any help would be appreciated!

+6
source share
1 answer
 document.getElementsByClassName("button2 redColor")[0].click(); 

You need to select an index because getElementsByClassName returns an array

+7
source

Source: https://habr.com/ru/post/947450/


All Articles