You are on the right track, but your form is submitted always because you are not canceling the events (or, in DOM Level 2+ parlance, you are not “preventing the default action for the event”).
function submitForm (button)
{
if (button.value == "Find")
{
/* open popup */
find();
}
else if (button.value == "Add")
{
/* stay in the same window */
}
return false;
}
<form action="/findNames">
<input type="submit" value="Add" onclick="return submitForm(this)"/>
<input type="submit" value="Find" onclick="return submitForm(this)"/>
</form>
(You should never name a form-related variable submit because if you are not careful that overrides the form object's submit method.)
The return value false, when returned to the event handler, prevents the default action for the click event. This means the user agent works as if the submit button was never activated in the first place, and the form is not submitted.
Another approach to solve this is to save a value identifying the clicked submit button in a variable or property, and check that value in the submit event listener. This works because the click event of the input (submit button) by definition happens before the submit event of the form:
var submitName;
function submitForm (form)
{
if (submitName == "Find")
{
/* open popup */
find();
}
else if (submitName == "Add")
{
/* stay in the same window */
}
return false;
}
function setSubmit (button)
{
submitName = button.value;
}
<form action="/findNames" onsubmit="return submitForm(this)">
<input type="submit" value="Add" onclick="setSubmit(this)"/>
<input type="submit" value="Find" onclick="setSubmit(this)"/>
</form>
(This is just an example. Try to minimize the number of global variables.)
Again, the return value false, when returned to the submit event handler, prevents the form from being submitted. You may want to return true instead if you explicitly want the form to be submitted after you handled the submit event. For example, you may want to validate the form and if validation was successful, display the server response in another frame, through the target attribute.
The advantage of event-handler attributes over adding event listeners in script code is that it is runtime-efficient, backwards-compatible, and still standards-compliant. The disadvantage is that you may have to duplicate event-handler code if the event does not bubble. (Not an issue here.)
Other people may say that a disadvantage of event-handler attributes is also that there is no separation between markup and function; however, you should make up your own mind about that. In my opinion, function is always tied to specific markup, and the jumping through hoops for working around different DOM implementations is seldom worth it.
See also: DOM Client Object Cross-Reference: DOM Events
The most important thing here is that, regardless of all client-side improvements that you make, the form stays accessible, i. e. that it still works with the keyboard even without client-side scripting. Your server-side script (here: /findNames) can work as fallback, then, and the client-side script can avoid unnecessary roundtrips, improving the user experience and reducing the network and server load.
nameattribute to theinputtag?