|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Detect Which button was pressed in Javascript
I have two submit buttons on a form which both call the same function onsubmit of the form.
I would like to know in the function which button I pressed. Here is the pseudo code I have so far function Validate(form) { //What is syntax here?? if (form.submitButton.pressed==true){ return true; } //What is syntax here?? if (form.clearButton.pressed==true){ return false; } } Thanks |
|
#2
|
||||
|
||||
|
this is not ASP question rather Javascript question - this post should be in the html/javascript forum.
anyways, this is quite simple: if (window.event.srcElement == form.submitButton) //code to handle submit button click... the window.event.srcElement returns the "source object", i.e. the object which raised the current event. in your case, it will be the button which caused the Validate() function to fire. |
|
#3
|
|||
|
|||
|
Thanks for your reply.
Unfortunately I don't think you have understood my query. It is not the buttons which call the Validate function. It is the asp page form which runs the validate function. (Hence why I posted the query in this forum.) i.e. <form action="simpleform.asp" method="post" name="form1" onsubmit="return Validate(this);" > <input type="submit" name="action" value="Submit" /> <input type="submit" name="clear" value="Clear All"/> So in your example window.event.srcElement will always be form1. ( I think) I need to know which of two submit buttons was pressed last (or has gotfocus or something like that) I hope this clarifys things a bit more and thanks again. |
|
#4
|
||||
|
||||
|
no you're wrong.
please try the code I've given and let me know how it goes. |
|
#5
|
|||
|
|||
|
I'm afraid I still stick by what I have said.
When I alert out window.event.srcElement.name in the validate function it always gives me form1 and not the button names. Although I'm not very good at asp/javascript this makes perfect sense to given that the srcElement detects the thing which calls the function. In my example as you can see it is the form onsubmit which calls the validate function. (NOT the the submit and clear buttons.) Nevertheless I have overcome my initial problem of knowing which button was last pressed by adding a onclick event on each of the buttons which set a variable value in the javascript. Thanks for all your help. Appreciate it. |
![]() |
| Viewing: ASP Free Forums > Programming > HTML, JavaScript And CSS Help > Detect Which button was pressed in Javascript |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|