Link to home
Start Free TrialLog in
Avatar of bowemc
bowemc

asked on

Simple detection of button clicked

Hi,

I'm running a javascript when <form id=onSubmit="return chk()">. What i want to do is detect which button has been clicked to cause the submit action. If its "ImageButton1" return true else if its "ImageButton2" contine and persom the rest of chk() whci is as below.

Thanks

function chk() {


  var val=document.getElementById("lblTotal").innerHTML;
var realVal=val.replace(/[b\<\>\€\/]/g,"");
var len = realVal.length;
var  val1 = realVal.substring(1,len);
var val2 = parseFloat(val1);
  if(val2>=25) {
    return true;
  }
  else {
    alert("You cannot proceed as your balance is less than E25.00. \nPlease select additional items before trying to proceed again. Thank you.");
    return false
  }
}

Avatar of mshogren
mshogren

Detecting which button is the active one is done differently from browser to browser.  Here is an example that should work everywhere though.  It is called by using chk(event);

function chk(e) {
  var elem = e ? e.target : window.event.srcElement;
  alert(elem.name + " " + elem.id);
  var val=document.getElementById("lblTotal").innerHTML;
var realVal=val.replace(/[b\<\>\€\/]/g,"");
var len = realVal.length;
var  val1 = realVal.substring(1,len);
var val2 = parseFloat(val1);
  if(val2>=25) {
    return true;
  }
  else {
    alert("You cannot proceed as your balance is less than E25.00. \nPlease select additional items before trying to proceed again. Thank you.");
    return false
  }
}

Avatar of bowemc

ASKER

ok to clarify i have:

<asp:imagebutton id="ImageButton2" Runat="server" height="15" border="0" ImageUrl="images/front_end_site_images/proceed_checkout.gif" width="66"></asp:imagebutton>

when this (or currently any button is clicked on the form the following line is called:

<form id=Form1 onSubmit="return chk()" method=post runat="server">   ------> onSubmit="return chk()"

which in turns calls the method i posted at the start....

I tried what you sugggested by entering

onSubmit="return chk(event)" and addint the first two lines suggested in the chk() metod ---- when i ran it in Firefox nothing happened - it was like chk() never got called - no msg box appeared


any ideas?
I can sort of see that there is a problem since the event that is being executed during chk is onsubmit which doesn't have a target element.  The workaround would be to have each button call the chk function and submit the form and remove it from the onsubmit altogether.
Avatar of bowemc

ASKER

how do i call chk() on the click event of just

<asp:imagebutton id="ImageButton2" Runat="server" height="15" border="0" ImageUrl="images/front_end_site_images/proceed_checkout.gif" width="66"></asp:imagebutton>


wouldn't this not work (obviously just from what you know of my application!) ... thanks again
As far as I know this does work.  Let me know if I am wrong.

<asp:imagebutton id="ImageButton2" Runat="server" height="15" border="0" ImageUrl="images/front_end_site_images/proceed_checkout.gif" width="66" onclick="chk(event);"></asp:imagebutton>
Avatar of bowemc

ASKER

Error!!

 Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1513: } expected

Source Error:

Line 321:<tr><td>&nbsp;</td><td><center><asp:imagebutton id="ImageButton2" Runat="server" height="15" border="0" ImageUrl="images/front_end_site_images/proceed_checkout.gif" width="66" onclick="chk(event);"></asp:imagebutton></center></td></tr>

Any more ideas? All welcome!!
I guess I am not so great with the asp stuff.
ASKER CERTIFIED SOLUTION
Avatar of timker
timker

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bowemc

ASKER

the line  clickId =  e ? e.target : window.event.srcElement;

the javascript module in firefox complians that e is not defined - which i think is true - what is e and where should i declare it?

thanks