Javascript: Automatically clicking a button?

57,449
document.getElementsByClassName("button2 redColor")[0].click();

You need select index, because getElementsByClassName return the array

Share:
57,449
KingPolygon
Author by

KingPolygon

Updated on October 19, 2020

Comments

  • KingPolygon
    KingPolygon over 3 years

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

    Here's 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 automatically clicking button2 using javascript.

    I'm using the following, but it's not working :/

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

    Any help would be appreciated!!