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.
Here is the HTML code including the JavaScript:
1 2 3 | <input type="checkbox" onClick="document.getElementById('theSubmitButton').click();">Check the box to simulate a button click <br> <input type="button" name="theSubmitButton" id="theSubmitButton" value="Button" onClick="alert('The button was clicked.');"> |
If the button is clicked directly, a JavaScript alert will pop up with the following message:
The button was clicked.
The onClick event handler for the checkbox will access the HTML DOM button object and invoke the click() method. This simulates the button click and invokes the alert.
If you found that my code was helpful in any way, shape, or form and would like to buy me a beer, please use the Donate button below =) Cheers!
Thanks!
i wanted to apply a focus functionality on my site http://www.askinterview.com can any one tell me how to use a focus..
Hi sumit,
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.
Allen
This only works in IE
Hi Smitty,
I have tested in IE, FireFox, and Chrome and they all worked for me. Can you please tell me which browser(s) failed to work and their version(s)?
Thanks,
Allen
how can we mimic the designed submit button? like when one press the it appears to be clicked.
May I ask why you can’t use:
or
Thanks! When submit() (for some unknown reason…) didn’t work, this is nice :)
[...] Usage of JavaScript, which may be turned off on some computers, plain HTML is faster by default, … [...]
Thanks a lOt!
Great! Solved my problem. :)
Thank you man. You guys make life easier for us.
I was looking for this exactly. Very helpful! Cheers!
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 ?
Thanks for the exemple by the way :)
(and sorry for my english)
Also it is possible this elegant simulation:
Play a song!
What if you have an control and you are trying to simulate the click event? I’m trying to fire a server-side event that’s why i need the asp:button.
You can’t use getElementById(“Button_ID”) right? Do you need the ClientID?
Thanks…
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. :-) )
Hi, I’m looking for something like this:
function fireEvent (x, y, type) {…}
Does somebody know’s any function like above?
Because I need to simulate an event at X,Y with the mouse.
Example: fireEvent( 100, 50, “mousedown” );
Thanks.
Can you replace the button with a hyperlink?
Yes, it should work!
http://www.webmasterworld.com/forum91/762.htm
Thanks but not is what I’m looking for.
I want to call “Mediabox” from a button
This is the link:
play flash video »
And this is the library:
http://iaian7.com/webcode/Mediabox
The link contains attributes:
<a href="http://iaian7.com/media/2007/06/2007vfx.flv" rel="mediabox[640 360]" title="flash video">play flash video »</a>
Very Nice!
But do you know how to set this kind of click on a link object with HREF instead ONCLICK?
Thanks.
Hi Henrique,
Yes, you can try the following instead:
Neat!
Nice code … thanks
its nice …thanx a lot