Prevent default behaviour of event in JQuery

Posted on 19-02-2013 18:01 by graham
This tutorial describes how to prevent a default behaviour of an event in JQuery.

Preventing default event


When you attach an function to an event in JQuery, you often want the default behaviour of that event to be totally overwritten. In order to do that, you can call the preventDefault() method on the event object.

For example, if you create a custom validation for your form, you don't want it to be submitted if there are any validation errors.
$('#myForm').submit(function(e) {
// do some stuff
// ...

// prevent the form from being submitted
e.preventDefault();
});

A full example


To see an example, take a look at our demo.
Comments

 

Add comment

Has this tutorial been helpful to you? Or do you see anything wrong? We appreciate your opinion!
Your comment:
Show formatting hints
HTML is disallowed, but in your text you can use the following markup
  • [code][/code] for a block of code
  • [tt][/tt] for inline code
  • [link]link href|link anchor[/link] for links
  • [b][/b] for bold text
Email:
+ Ask a question
If you have a technical question related to programming and computers, ask it here. Other users will help you solve it!
Unanswered questions
Share your knowledge by helping others solve their problems