views:

278

answers:

1

The HTML form shown below does not work as expected in Opera (Version: 9.52). The form does not have an onsubmit attribute nor does it have an input element with type=submit. It just has two input elements of type=button and both of them onclick calls a js method where I want the user to confirm the submission. If I remove the confirm() call, it works perfectly fine. And in all other browsers (FF2, FF3, IE7) it is working fine.

Any pointers ?

<script type = "text/javascript">
function userSubmit(submitStatus)
{
    // Omitted code that uses the parameter 'submitStatus' for brevity
    if(confirm('Are you sure you want to submit?'))
     document.qpaper.pStatus.value = something;
    else
     return;  
    document.qpaper.submit();
}
</script>
<form  name = "qpaper" method = "post" action = "evaluate.page">
    <input name = "inp1" type = "button" value = "Do This" class = "formbutton" onClick = "userSubmit(false)">
    <input name = "inp2" type = "button" value = "Do That" class = "formbutton" onClick = "userSubmit(true)">
</form>
+2  A: 
  1. Never use document.nameofsomething. It's an outdated technique that has patchy support in 21st century browsers. For forms use document.forms.nameofform.

  2. Don't use onclick on buttons unless you need Javascript to behave differently depending on which button was pressed. If you just want to validate form or confirm submission, then use <form onsubmit> instead.

    <form onsubmit="return confirm('Sure?')">
    

    And this way you don't even need form.submit(). If <form onsubmit> returns false, submission will be aborted.

  3. You don't even need to find that form.
    In <button onclick> form will be this.form.
    In <form onsubmit> form will be in this variable.

porneL
@porneL: Thanks for the tips. Regarding your second point, I am using onclick events only because I want the behavior to be different depending on the button that is pressed. I omitted the code that uses the parameter passed to the JavaScript function for brevity.
Vijay Dev