View Full Version : Detecting which button was pressed after and onchange event

03-28-2003, 02:55 PM
I have a form created in an ASP. On the form are several buttons, one of which is SAVE. Also on this form is a text box that has an ONCHANGE event. This ONCHANGE event executes a function that manipulates the URL. When the value in the textbox is changed and the SAVE button is pressed without tabbing out of the textbox, the ONCHANGE event is triggered. Then processing returns to the form as though the SAVE button is just acting like a tab. The SAVE button is defined as so: <input type=Button name=dataaction value=save>. I tried to detect the button by using the javascript document.forms[0].dataaction.value but there is nothing there.

I need to be able to know that the button pressed was the SAVE button and not one of the other ones on the form. Can anyone suggest how to detect the button pressed??

03-28-2003, 06:11 PM
...use an 'onclick' event handler perhaps?

<input type="button" name="dataaction" value="Save"
onclick="jav&#97;script:alert('You clicked the Save button')" />

Just replace the jav&#97;script:alert with your function call like onclick="myFunction()"

Hope that helped. Cheers

03-28-2003, 06:41 PM
That is pretty much what I am doing.
<input type="button" name="dataaction" value="Save" onclick="myfunction()">
Essentially myfunction() is:

var ButtonClicked = document.forms[0].dataaction.value;
alert ("Button clicked is: " + ButtonClicked);

The result is : ButtonClicked is undefined.

03-28-2003, 07:08 PM
try this... Revisited

<script language="javascript" type="text/javascript">
function buttonTest(btn){
alert("Button clicked is: " + btn.value)

...with this form in the body:

<input type="button" name="dataaction" value="Save"
<input type="button" name="dataaction" value="Help"
<input type="button" name="dataaction" value="Close"