0

Here is my code

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("form").submit(function(event){
    event.preventDefault();
    alert("Submit prevented");
 });
});
    function ShowAlert()
   {alert("Form Submitted");}
</script>
</head>
<body>
<form action="">
First name: <input type="text" name="FirstName" value="Al"><br>
Last name: <input type="text" name="LastName" value="McCoy"><br>
<input id="ffff" type="submit" value="Submit" onclick="ShowAlert()">
</form> 
</body>
</html>

The expected behaviour is only "Submit prevented" will be alerted, but in real "Form Submitted" and "Submit prevented" are both alerted, in the given order. But I want only the "Submit prevented" to be alerted. Is there a way to prevent onclick from being executed?

Swagatika
  • 23
  • 5

1 Answers1

0

You can only remove existing onClick handler from your submit button. Something like this:

$(document).ready(function(){
    $('#ffff').off('click');
    $("form").submit(function(event){
        alert("Submit prevented");
    });
});
name_no
  • 354
  • 2
  • 9
  • it is not working still it is showing the same result – Swagatika Dec 13 '15 at 07:28
  • You have to do it outside of submit handler, see edited answer – name_no Dec 13 '15 at 07:37
  • I tried using jsfiddle ,but still it is unable to deactivate the onclick handler.you can also check the same. – Swagatika Dec 13 '15 at 08:57
  • Ok, I see, it's my mistake. off() can only disable jquery's handlers. You probably have to unset onclick attribute as described here: http://stackoverflow.com/questions/33614561/jquery-disable-onclick-event-using-off-not-working – name_no Dec 13 '15 at 10:06