Pages

Simulate a button click via JavaScript

There is a rare need for this type of functionality but I have found myself in a couple of situations where I needed it. Here is a quick demo of a button click that is simulated from another event handler. In this case, the button click is invoked by checking a checkbox.

Demo

Check the box to simulate a button click

Here is the HTML code including the JavaScript:

<input type="checkbox" onClick="document.getElementById('theSubmitButton').click();">Check the box to simulate a button click

32 thoughts on “Simulate a button click via JavaScript”

I have 5 buttons with values 1, 2, 3, 4, 5 respectively. what i want is when i click a button in one webpage the button with the same value on another webpage should get clicked automatically. This is like communication between two webpages through a server. How can i do this ?

Its like a multiplayer game where button clicks should be same on both webpages which are on the same domain and button once clicked cannot be reverted back. But what i thought is using socket.io for this but i am unable to implement it and get what i wanted.

https://www.youtube.com/watch?v=oC5ikKILovk
As you can see in this video when once clicked it is marking the two webpages with same symbol. Similar to that i want buttons in place of those boxes(marking areas) and when one button is clicked on one webpage the same button should get clicked on another webpage.

In this example, the site that is processing example.com would look for the “button” parameter and see which value has been received: in this case, “secondOne”. Then the site can render the page accordingly from the server or on the client-side with something like jQuery (which is used in the example post I sent to you).

Which element do you want to apply the focus on? I am assuming the goal is to allow the user to begin typing something and gave the input go into the element without first having to click on it such as a search bar. Please correct me if I’m wrong.

Is that possible to send a parametter to the submit button ? I have a VB function in the code beheind and i had multiple button who are supposed to active the VB function but whith a parametter. How can i do that ?

This is great and halfway to what I need. I actually want to simulate a button push when a certain key is pressed.
What I have is a web page that shows video clips. I have “prev” and “next” buttons. Sometimes the buttons are (intentionally) off the visible page. What I want to do is capture keys to simulate a button click. The button push actually results in the page being reloaded with the prev/next video clip. So I’m not using checkboxes just a plain old key capture. The way it works is that once the page is loaded if I click anywhere on the page (document)it gives it focus and then when I click the appropriate key (P/N) it does what I would expect. However because it reloads the page I lose focus and so have to click on the page again. (Yeah probably bad design in the first place but it is what it is at this point.) So I tried to get around this by something like or but it doesn’t seem to work. So my question is how do I set focus on the document itself after it loads? (Oh and this is a page within a frameset to boot. :-) )