-2

I am programming a script that should click buttons on a website, which dont have a onclick event. for example:

<button class="button exButton but">Example</button>

I try to automate them so you don't have to click them all by yourself, i am trying to figure out how i can click them in javascript.
First of all document.getElementsByClassName("button")[0].click doesn't work
I hope you guys can help me!

Greetz Julian

Julian Be
  • 128
  • 1
  • 3
  • 13
  • `document.getElementByClassName("button")[0].click` you aren't calling that function. – VLAZ Aug 14 '16 at 21:02
  • `getElementsByTagName` – dandavis Aug 14 '16 at 21:02
  • @dandavis `button` is also a class name. – Sebastian Simon Aug 14 '16 at 21:03
  • 2
    Possible duplicate of [How to trigger event in JavaScript?](http://stackoverflow.com/questions/2490825/how-to-trigger-event-in-javascript), also a duplicate of [How to Trigger a Click Using jQuery](http://stackoverflow.com/questions/5811122/how-to-trigger-a-click-on-a-link-using-jquery). What you want to do is `$('.button').trigger('click')`, but since your button may not even have a click handler it's doubtful it will do anything. To rephrase, since there is no "onclick" event, clicking them is **supposed** to do nothing. Here's the "correct" answer: `;` – Tibrogargan Aug 14 '16 at 21:03
  • @Xufox: good eye; both should work – dandavis Aug 14 '16 at 21:03
  • so first off all, thanks for all the replies, but i got the spelling right, it was just a spelling mistake in the question ^^ thanksto @Tibrogargan for the question links o/ – Julian Be Aug 14 '16 at 21:07
  • If the DOM is loaded and you’re using `document.getElementsByClassName("button")[0].click();`, it _should_ work. – Sebastian Simon Aug 14 '16 at 21:07

1 Answers1

0

You can do that using JQUERY .trigger() function .

$( "button.exButton" ).trigger( "click" );

A call to .trigger() executes the handlers in the same order they would be if the event were triggered naturally by the user:

Nihar Sarkar
  • 1,187
  • 1
  • 13
  • 26