7

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!!

2
  • document.getElementsByClassName("button2 redColor")[0].click(); Commented Jun 17, 2013 at 9:37
  • Thank you LightStyle, I just wasn't sure how to grab the a button already inside another class! Commented Jun 17, 2013 at 9:42

1 Answer 1

11
document.getElementsByClassName("button2 redColor")[0].click();

You need select index, because getElementsByClassName return the array

Sign up to request clarification or add additional context in comments.

3 Comments

An answer - in my opinion - is not necessary in this case. Anyway, your solution won't work because it is getElements and not getElement. Pay attention to the typos(even of the OP)!
@Zenith .click() exists even for DOM elements, it's not a jQuery function(in this case)
@NiccolòCampolungo getElements is correct. it returns an array of elements, in case you need to retrieve all elements of the same class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.