简体   繁体   中英

How to check if a button has been clicked using javascript?

Here i need to check a button click using javascript ie)if button A is clicked i will call a javascript function and if button B is clicked i will call another javascript function.

if(document.getElementById('imgBTNExportPPT').clicked == true)
{
   ShowDialogExportPPTPOPUP();
}
else if(document.getElementById('btnShowModal').clicked == true)
{
   ShowDialogPrintPOPUP();
}

and

      <asp:ImageButton ID="imgBTNExportPPT" runat="server" Width="15" Height="15" border="0"
                                                            OnClick="imgBTNExportPPT_Click" ImageUrl="~/Images/PPT_icon.png" />
  <asp:ImageButton ID="btnShowModal" runat="server" Width="15" Height="15" border="0"
                                                         ImageUrl="~/Images/Print_icon.png" onclick="btnShowModal_Click"  />

is it possible??any suggestion??

Try this :

function buttonClicked(choice)
{
   if(choice == 'A')
   {
      ShowDialogExportPPTPOPUP();
   }
   else if(choice ==  'B')
   {
      ShowDialogPrintPOPUP();
   }
}

HTML code should be like this :

<input type='button' value='ButtonA' onclick="buttonClicked('A')" />
<input type='button' value='ButtonB' onclick="buttonClicked('B')" />

If it's a server side control then you can do it in two ways :

  <asp:ImageButton onClientClick="buttonClicked('A')" ID="imgBTNExportPPT" runat="server" Width="15" Height="15" border="0"
                                                            OnClick="imgBTNExportPPT_Click" ImageUrl="~/Images/PPT_icon.png" />
  <asp:ImageButton onClientClick="buttonClicked('A')" ID="btnShowModal" runat="server" Width="15" Height="15" border="0"
                                                         ImageUrl="~/Images/Print_icon.png" onclick="btnShowModal_Click"  />

OR (in C#)

{
 imgBTNExportPPT.Attributes.Add("onclick", "buttonClicked('A')");
 btnShowModal.Attributes.Add("onclick", "buttonClicked('B')");
}

http://msdn.microsoft.com/en-us/library/7a9d6h4f(v=vs.80).aspx

You have to use OnClientClick attribute of ImageButton to assign javascript function. You can pass this in javascript call to get the button object being clicked.

OnClientClick="TestClick('A', this);" add this in button

In .aspx Page

<asp:ImageButton OnClientClick="TestClick('A', this);" ID="imgBTNExportPPT" runat="server" Width="15" Height="15" border="0"                                                                  OnClick="imgBTNExportPPT_Click" ImageUrl="~/Images/PPT_icon.png"  />

<asp:ImageButton OnClientClick="TestClick('B', this);" ID="btnShowModal" runat="server" Width="15" Height="15" border="0"
                                                                 ImageUrl="~/Images/Print_icon.png" onclick="btnShowModal_Click"  />

In Script

function TestClick(choice, btnClicked)
{
   alert(btnClicked.id + " clicked");
   if(choice == 'A')
   {
      ShowDialogExportPPTPOPUP();
   }
   else if(choice ==  'B')
   {
      ShowDialogPrintPOPUP();
   }
}

You could add an identifier or just call a function in the onclick event. For example:

<script type="text/javascript">
    function onButtonClick(button)
    { 
        alert("button " + button + " clicked!"); 
    }
</script>

<button id="ButtonA" onclick="javascript:onButtonClick('buttonA'); return false;" />
<button id="ButtonB" onclick="javascript:onButtonClick('buttonB'); return false;" />

Here is a good example. Transfer your code as the callback to runOnce and create an onclick event handler like the one below to restrict your function to one call:

function runOnce( callback ) {

  var done = false;

  return function() {

    if ( !done ) {
      done = true;

      callback();

    }

  };

}

var one = runOnce(function() {
    alert('Once');
}),
    two = runOnce(function() {
    alert('Once only too.');
});

document.getElementById('first').onclick = one;
document.getElementById('second').onclick = two;

Here is a DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM