Kaiusee
Kaiusee

Reputation: 1333

JavaScript how to program button to act like a mouse click?

I'm trying to program a button to act like a mouse click event using ONLY JavaScript (NO jQuery)

For example if you have:

<h2> Please select your fav: </h2>
<input type="checkbox" id="check1" class="checkbox" /><label for="check1">C++</label>
<input type="checkbox" id="check2" class="checkbox" /><label for="check1">Java</label>
<input id="submitAnswer" type="button"  value = "Submit your Answer" onClick="submitAnswers()">/input>

<input id="mouseClick" type="button"  value = "CLICK" onClick="mouseClick()"></input>

So, I have a focus method, which checks what element has focus. I need to program another button to act like the mouse click, so in case of clicking on the checkboxes, it should check/uncheck it. and if the focus on the submit button, it should trigger the function.

Upvotes: 1

Views: 1824

Answers (1)

Naftali
Naftali

Reputation: 146310

var input = document.getElementById('submitAnswer');

input.click(); //simulate click event

Upvotes: 1

Related Questions