Is it possible to make two .click method calls in javascript

I have the following javascript code:

function ClickButtons() { document.getElementById('Button1').click(); document.getElementById('Button2').click(); } 

Button2 is pressed. If I cancel the order of the operators, then Button1 (which will be called 2nd) works.

FYI (do not think that this affects this problem, here for further information): clicking the button makes ajax updates / partial pages (they call data services and fill in the data on the page)

EDIT: The setTimeout solution works, but lowers the performance limit. I looked a little, and the two buttons are the asp.net buttons and are inside the update panels. If I click on them sequentially, they work fine, but if I click one and then quickly press the second (before the first is finished), then the second will work, and the first will fail. Is this apparently a problem with update panels and asp.net? Is there anything I can do on this front to enable the above javascript and avoid the setTimeout option?

0
source share
3 answers

It should work. Try placing a setTimeout call for the second button after pressing the first button. It's ugly, but maybe it will work.

 function clickButton() { document.getElementById('#button1').click(); setTimeout(button2, 300); } function button2() { document.getElementById('#button2').click(); } 
+1
source

How to use fireEvent () IE function?

 function ClickButtons() { document.getElementById("Button1").fireEvent("onclick"); document.getElementById("Button2").fireEvent("onclick"); } 

Working example (tested in IE6):

 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Click Test</title> </head> <body> <button id="Button1" onclick="alert('Button1 Clicked');">Click 1</button> <button id="Button2" onclick="alert('Button2 Clicked');">Click 2/button> <script type="text/javascript"> document.getElementById("Button1").fireEvent("onclick"); document.getElementById("Button2").fireEvent("onclick"); </script> </body> </html> 

Since your use of click() , as far as I know, depends on IE, so does this answer. Events are fired differently in different browsers, but it should not be too difficult to wrap and hide the difference.

+1
source

They should both fire the click event. What happens if you remove your ajax requests and other heavy loads and just put a warning or console.log in each event listener. Oh, and how do you listen to events?

0
source

All Articles