1

I have added a click listener to the Gmail "Send" button and am using event.preventDefault() and event.stopPropogation() to try and stop the email from sending, however, the email is still sending and the try catch blocks arent registering any errors.

$("div:contains('Send')").last().click(function(event){
    try{
        console.log("preventing default");
        event.preventDefault();
    }catch(error){
        console.log(error);
    }
    try{
        console.log("stopping propogation");
        event.stopPropagation();
    }catch(error){
        console.log(error);
    }
    alert("Hello");
});

This is the code I am using, I'd appreciate any input on why this isn't stopping the event execution.

Irtza.QC
  • 1,026
  • 2
  • 10
  • 23

1 Answers1

0

Let me explain you those two things,

event.preventDefault() will prevent the default actions of event source. For example, on click of Anchor, navigation to another page.

Please have a look at http://api.jquery.com/event.preventdefault/

Next, event.stopPropagation() will prevent event to get triggered by parent node to child node

Also have a look at https://api.jquery.com/event.stoppropagation/

In any case, javascript function you have written will be executed.

schoolcoder
  • 1,546
  • 3
  • 15
  • 31
  • "why isnt it working? " means?? .. if alert("hello") is working, then it is working as expected. Neither event.preventDefault() nor event.stopPropagation() is like return statment. – schoolcoder Nov 09 '15 at 08:18
  • Hope it will explain more for you. http://stackoverflow.com/questions/1357118/event-preventdefault-vs-return-false?rq=1 – schoolcoder Nov 09 '15 at 08:20