Getting button id in JS

O

Oleg Konovalov

Hi,

I am working on Struts/HTML/Javascript application.

Have 2 buttons on the page, Save and Cancel,
both are supposed to bring me to the same page,
but Save also performs validation, and Cancel - should not.
Both buttons are of Submit type and have same name ("SubmitValue"), e.g.:
<input type="submit" id="SaveBtn" name="submitValue" value="Save">

So how do I distinguish which button was clicked on Submit for the form:
<form name="input" onClick="if (??? = "SaveBtn") return validate()" >,
how do I capture that id=SaveBtn in JavaScript ?

Thank you in advance,
Oleg.
 
R

RobG

Hi,

I am working on Struts/HTML/Javascript application.

Have 2 buttons on the page, Save and Cancel,
both are supposed to bring me to the same page,
but Save also performs validation, and Cancel - should not.
Both buttons are of Submit type and have same name ("SubmitValue"), e.g.:
<input type="submit" id="SaveBtn" name="submitValue" value="Save">

So how do I distinguish which button was clicked on Submit for the form:

Change either the name or value of one of the buttons so your server
can tell which one was clicked.
 
O

Oleg Konovalov

RobG said:
Change either the name or value of one of the buttons so your server
can tell which one was clicked.
Values arre different, one is "Save", other one is "Cancel"
How do I get it there:
<form name="input" onClick="if (??? = "SaveBtn") return validate()" > ?
 
D

Darko

Values arre different, one is "Save", other one is "Cancel"
How do I get it there:
<form name="input" onClick="if (??? = "SaveBtn") return validate()" > ?

You got it completely wrong. First of all, you don't put <form
onclick..> but <input type="submit" onclick...>. Here's a short
example (untested) how you should go:
<script type="text/javascript">
function save()
{
// check everything
if ( everythingOk )
return true;
return false;
}

function cancel()
{
return true;
}
</script>
<form action="...">
<input type="submit" value="Save" name="save" onclick="return
save()" />
<input type="submit" value="Cancel" name="cancel" />
</form>

If you don't need any validation on cancel, then just don't handle it
in javascript,
but let it be a normal submit button. save(), however, should do the
validation, and if it
returns false, then submitting won't happen. If it returns true, it
will.

Now, what I really don't understand - why did you put both buttons
have the same name? If you let
them have the same name, then the server-side script won't be able to
know what button was pressed.
Having them have different names, like above, the server-side script
will be able to distinguish which
button was pressed and act accordingly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top