88883

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

Question:

I have the following javascript code:

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

only Button2 seems to be clicked. If I reverse the order of the statements then only Button1 (which would be called 2nd then) seems to work.

FYI (don't think this is impacting this issue, here for futher information): The button clicks are doing ajax/partial page updates (they call data services and populate data on the page)

EDIT: The solution setTimeout works, but puts an lower bound on performance. I've done a bit more looking and the two buttons are asp.net buttons and are inside update panels. If I click them sequentially they work fine, but if i click one and then quickly click a second one (before the first has completed) then the second one will work and the first will fail. This appears to be an issue with update panels and asp.net? Is there anything I can do on that front to enable the above javascript and avoid the setTimeout option?

Answer1:

It should work. Try putting a setTimeout call for the second button after the first button is clicked. It's ugly but maybe it will work.

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

Answer2:

How about using IE's fireEvent() 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() is to my knowledge IE-specific, so is this answer. Events are fired differently in different browsers, but it shouldn't be too tough to make wrappers and hide the difference.

Answer3:

They should both fire a click event. What happens if you remove your ajax requests and other heavy lifting and just put an alert or console.log in each event listener. Oh, and how are you listening to events?

Recommend

  • Intellij Idea Terminal shortcut not working
  • Sending rails errors to rspec output
  • coldfusion variables into powershell
  • Mongodb update() vs. findAndModify() performace
  • Calling C function from lua
  • Creating UDF with VSTO in Excel
  • How to set an entity field that does not exist on the table but does exists in the raw SQL as an ali
  • Highcharts - Column chart with empty columns for date in x-axis
  • It is possible use the same sql azure instance from two different cloud service of two different sub
  • how to query for min or max inet/cidr with postgres
  • Creating a Multi-Step Modal Using Jquery
  • Why cout is producing no output on Code Blocks?
  • C# List of Panels
  • sweetalert2 inputoptions from file in select example
  • Request response issues in biztalk
  • cordova is not defined - cordova.js has already been loaded :: Ionic
  • Clear fused location provider's location for testing
  • How to return DataSet (xsd) in WCF
  • TextToSpeech.setEngineByPackageName() triggers NullPointerException
  • Debug.DrawLine not showing in the GameView
  • azure media services - The request body is too large and exceeds the maximum permissible limit
  • Time complexity of a program which involves multiple variables
  • Custom validator control occupying space even though display set to dynamic
  • Row Count Is Returning the incorrect number using RaptureXML
  • Android full screen on only one activity?
  • Allowing both email and username for authentication
  • Get one-time binding to work for ng-if
  • Is my CUDA kernel really runs on device or is being mistekenly executed by host in emulation?
  • How to recover from a Spring Social ExpiredAuthorizationException
  • ILMerge & Keep Assembly Name
  • Large data - storage and query
  • Adding custom controls to a full screen movie
  • How do you troubleshoot character encoding problems?
  • WOWZA + RTMP + HTML5 Playback?
  • json Serialization in asp
  • embed rChart in Markdown
  • Django query for large number of relationships
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app