0

I have 2 questions, both regarding clicking via js.

So I am on a site and I wish to automate a physical click on 2 buttons, I learned that if we getElementbyId.click() we can do this. But both these buttons do not have an ID. I tried coordinate clicking and it doesn't work and also by the class but to no avail.

<td data-pick="red" class="red" rowspan="2"></td>

How do I click on this?

and also

<button type="submit" class="btn btn-default btn-success">GO</button>

and this.

document.getElementsByClassName doesn't work :(

5
  • 1
    What do you mean document.getElementsByClassName doesn't work? If you call document.getElementsByClassName('red') you'll get a collection which contains the <td>. Commented Mar 11, 2016 at 19:00
  • have you set up a handler to run when the element is clicked? also, you said these don't have an id. why not? Commented Mar 11, 2016 at 19:01
  • @MikeC I think he means it doesn't work if you call click() on the HTMLCollection which is returned. It has to be iterated over. Even if there's only 1 element. Commented Mar 11, 2016 at 19:02
  • Can you specify an id or a name for the elements? Commented Mar 11, 2016 at 19:04
  • Sorry. I'm new to this, so i don't know if you guys would be able to see this, so I'm tagging @MikeC when i use getElementsByClass('red'), i get multiple results, with all corresponding td with class red. But i only want to click the one with data-pick red. There can be td data-pick="1" class "red",td data-pick="2" class "red",td data-pick="3" class "red" but what if i only want to click td data-pick="red" class "red". Thank you so much for the help provided Commented Mar 12, 2016 at 3:28

3 Answers 3

1

Give this a try

document.getElementsByClassName('red')[0].click();

Why [0] ? because getElementsByClassName returns classes (matching DOM elements) in form of an array, so [0] is the index here :-) And for your second button you can trigger the click using

document.querySelector("button[type=submit]").click();

By the way if you are using jquery then why don't you use

$(".red").click();
$("button[type=submit]").click();

Anyways both solutions should work. Hope that helps :-)

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

1 Comment

You're welcome :-) Mark the answer as accepted so that it may help others as well..
1

Use jQuery..

$('td[class="red"], button[class="btn btn-default btn-success"]').click();

1 Comment

Thank you for the input. Well received and i learnt something!
1

It is possible to trigger a click using document.getElementsByClassName but you have to understand what you get back from document.getElementsByClassName is not an individual element, but a list which you can iterate over.

You can trigger a click for every item in a collection by doing the following:

var redClassElements = document.getElementsByClassName('red');

for(var i=0;i<redClassElements.length;i++) {
    redClassElements[i].click();
}

This will do nothing if you haven't assigned a click handler to the element click() is being called on though.

Also, do you really want to trigger a click on multiple elements? Your best bet is to assign the element an id as has been suggested to you. You can then use document.getElementById('theId').click().

1 Comment

Oh My Gosh. Thank you so much! i used this method but i replaced i with the number i wanted.

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.