Click to See Complete Forum and Search --> : HTML Form Buttons click detection


applejack
October 26th, 2005, 11:21 PM
I have a form with 2 buttons - edit and delete.

When the edit button is clicked, i want it to run through a validation sequence which is defined within validate(). It checks that 'existCat' (a select list) has something selected and then that something has been entered into 'newname' (a text field). This part works fine

When the delete button is clicked, I only want it to check if an item in the select list has been selected and to ignore the newname field being blank.

I've done some searching on the net, but I can't find anything that can tell me how to detect which button was pressed so that i can define what to do in each situation.

Any help would be appreciated.

olivthill
October 27th, 2005, 03:28 AM
I suppose you have:
<input type=button value="edit" onClick="validate()">
<input type=button value="delete" onClick="validate()"> and in the validate() function you don't know which button has been clicked.

A solution is calling two different functions:
<input type=button value="edit" onClick="onclick_edit()">
<input type=button value="delete" onClick="onclick_delete()">
In onclick_edit() and in onclick_delete, you would first set a global variable showing which button is clicked, then call validate():

<script language=javascript>
var from_buttom;
function onclick_edit()
{
from_button="edit";
validate();
}

function onclick_delete()
{
from_button="delete";
validate();
}

function validate()
{
...
if (from_button=="edit")
...
}
</script>

PeejAvery
October 27th, 2005, 10:17 AM
<input type=button value="edit" onClick="validate()">
<input type=button value="delete" onClick="validate()"> and in the validate() function you don't know which button has been clicked.
Well, you can somewhat cheat this...

<script language="JavaScript">
function validate(which){
if(which=="edit"){window.alert("Edit");}
if(which=="delete"){window.alert("Delete");}
}
</script>

<form name="test">
<input type=button value="edit" onClick="validate(this.value)">
<input type=button value="delete" onClick="validate(this.value)">
</form>