jQuery API

event.preventDefault()

event.preventDefault() Returns: undefined

Description: If this method is called, the default action of the event will not be triggered.

  • version added: 1.0event.preventDefault()

For example, clicked anchors will not take the browser to a new URL. We can use event.isDefaultPrevented() to determine if this method has been called by an event handler that was triggered by this event.

Example:

Cancel the default action (navigation) of the click.

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
  
<a href="http://jquery.com">default click action is prevented</a>
<div id="log"></div>

<script>
$("a").click(function(event) {
  event.preventDefault();
  $('<div/>')
    .append('default ' + event.type + ' prevented')
    .appendTo('#log');
});
</script>

</body>
</html>

Demo:

User Contributions

  • Support requests, bug reports, and off-topic comments will be deleted without warning.

  • Please do post corrections or additional examples for event.preventDefault() below. We aim to quickly move corrections into the documentation.
  • If you need help, post at the forums or in the #jquery IRC channel.
  • Report bugs on the bug tracker or the jQuery Forum.
  • Discussions about the API specifically should be addressed in the Developing jQuery Core forum.
  • Thonixx
    Hello..

    I'm using this feature for preventing my form of submitting. After that I made something with Javascript but after this I wanted my form submitting. But that doesn't work because I have the preventing feature in the code.

    How can I prevent the element for submitting for only a defined time?
    Or how can I make only a delay by the default action of the form element, so that the default function has a delay from aproximate one or two seconds?
  • You could do something like this:

    $('#myform').submit(function(event) {
    event.preventDefault();
    var self = this;
    window.setTimeout(function() {
    self.submit();
    }, 2000);
    });
  • Ingvi101
    hi, Im trying to do the same thing but with a navigation link (anchor) instead of submiting a form.
    So first I want to disable the link, perform a little effect (slideUp) and then actually go to the link, can you/someone help me out?

    $('ul#nav li a').bind({
    click : function(event){
    event.preventDefault();
    $('ul ul').slideUp(1000);
    // ??
    }
    });
  • my script as solution for ur problem:

    $("#ELEMENT_WHICH_AFFECT_THE_SLIDEUP")

    .click(function(event){

    event.preventDefault(); // prevents link to be redirected

    var time = 1000; // time for slideup effect

    var url = $(this).attr("href"); // pick url for redirecting via javascript


    $("#ELEMENT_TO_SLIDE_UP").slideUp(time); // effect


    window.setTimeout(function(){document.location.href=url;}, time); // timeout and waiting until effect is complete

    return -1;

    });

    Look here to see the effect working: http://white-tiger.ch
  • Champs
    @Onur: one advantage of preventDefault() is that it can be called anywhere, not just at exit. Any errors caused by your handler after making that call will appear in the browser's console and not be cleared away when it navigates to the next page. Firebug has persistence now, but that's not a universal feature.
  • Banning Stuckey
    I'm using this to disable keydown scroll
    $(document).keydown(function(event){
    // Move Down
    if(event.keyCode == '40'){
    event.preventDefault();
    var posY = $('#'+selectedTxtID).css('top');
    posY = parseFloat(posY);
    var newPosY = posY + 1;
    $('#'+selectedTxtID).css('top', newPosY+'px');
    }
    })

    It works in all browsers EXCEPT Opera... though Opera isn't a big deal I can't help but to want it to work in all browsers LOL...
  • Quentin
    Try to use keypress event. preventDefault() works in keypress in Opera, too.
  • banningstuckey
    Yes i had to add copy and paste all events under keydown and put them in keypress also to make up for Opera's lack of recognizing keydown... however i need keydown also because, i believe IE didn't recognize keypress... I can't remember now im to far along in my project LOL.. anyway issue was resolved by using keypress & keydown

    EDIT: It looks like opera recognizes the arrow keys for scrolling the browser window on keypress and not keydown like all the others. So opera is seeing keydown BUT NOT for scrolling the browser window, thats keypress
  • nnnnnn321
    You mention doing a copy-paste to duplicate the keydown handler for keypress. If both handlers do exactly the same thing you can use the .bind() method to list several events at once as follows:

    $(document).bind("keydown keypress", function(event) {
    // your code
    });

    (P.S. Opera's use of the keyboard is just weird, even without scripting issues, e.g., the tab key doesn't go to A elements.)
  • Aravind
    My webpage has scrollbars. When I make custom operation using $(document).bind("keydown keypress", function(event), it does not stop the scrolling. Its a kinda wierd to look. I tried all return false , e.preventDefault() and e.stopPropagation()..nothing works.
  • If you're using $(document).bind("keydown keypress", function(event) then e.preventDefault() won't work try.. event.preventDefault()
  • what is the difference between using preventDefault and return false? the code produces same behaviour if we replace it like this:

    $('<div>')
    .append('default ' + event.type + ' prevented')
    .appendTo('#log');
    return false;</div>
  • The Feeblizer
    According to John Resig:
    return false does e.preventDefault() and e.stopPropagation()