Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Jquery Return false, event.preventdefault and event.stopPropagation

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 783
    Comment on it

    Return False :- Basically returning false is a way to tell your action (like button click event, submit and load etc. ) is not firing. Return false do the three basic things

    1. event.preventDefault();
    2. event.stopPropagation();
    3. Stops the callback execution and returns immediately when called.

    Example:-

    $(document).ready(function(){
       $("#buttonSubmit").click(function(event){
         $("body").append($(this).attr("href"));
          return false; // "cancel" the default behaviour 
        })
    });
    

    Prevent Default :- The prevent Default prevents the default action the browser makes on that event.
    If this method is called, the default action of the event will not be triggered.
    For example, clicked anchor will not take the browser to a new URL.
    If any method or function has been called by any event handler and we want to triggered the event then we can use event.isDefaultPrevented().

    $("a").click(function(event) {
        event.preventDefault();
       $('#div1').append('default ' + event.type + ' prevented').appendTo('#InfoMessage');
     });
    

    Stop Propagation :- This will stops the event from bubbling up the event chain. Example:-

    $("p").click(function(event){
        event.stopPropagation();
        alert("The paragraph element was clicked.");
    });
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: