lunes, 6 de agosto de 2018

[jQuery] How to cancel an on-click event

Suppose you have the following html button



<button type="submit" class="btn" id="cancel_me">
  Cant Click Me!
</button>


And you want to prevent the default behaviour using jQuery in order to perform some other action

Here is the code that you need to do:


$(function() {
    $("#cancel_me").click(function(e) {
        e.preventDefault();
        /*Do Things*/
    });
})


And where it says "Do Things" insert the new behaviour you want to override with.

The preventDefault() method does the magic trick!

No hay comentarios:

Publicar un comentario