-1

I am new to Jquery custom events , was just reading the documentation and I decided to make my own custom event have a look below :

HTML :

<a href="http://stackoverflow.com/">Hello</a>

JQuery :

$(function () {

    // kabhoom = $.Event('click');  

    $('a').on( 'kabhoom' , function(e){
        console.log(e.target);
        return false;
    })

    $('a').click(function(){
        $(this).trigger('kabhoom');
    })

});

EDIT :: To sum up my difficulty in the above code returnfalse does't work ! the user is still able to navigate to the links in a.

now the glinch here is that the return false or the preventDefault works here , WHY ?

I experimented a bit and when I shift the logic to the

$('a').click(function(){
    $(this).trigger('kabhoom');
    return false;
})

it works , but inside my custom event listener , my code does't work . can somebody tell me why ? I am more interested in how Jquery works and that's why I ask this question ! .

Tenali Raman
  • 380
  • 5
  • 16
  • check out this topic: http://stackoverflow.com/questions/1357118/event-preventdefault-vs-return-false – max li May 27 '15 at 10:43

1 Answers1

8

Return exits the function with that return value. your preventDefault never gets reached.

    console.log(e.target); // Log stuff
    return false; // Exit function
    e.preventDefault(); // unreachable

Also in your second codeblock you never defined e. So even if it would reach the e.preventDefault(); line it would fail with Uncaught ReferenceError: e is not defined

Edit

You need to check if the propagation has stopped in

var content = $('#frut');
$(function () {

    // kabhoom = $.Event('click');  

    $('a').on( 'kabhoom' , function(e){
        // Are we good to go? Or has it stopped?
        if(!e.isPropagationStopped()) {
            content.append("Target = "+e.target+"<BR/>Cancelling Events<br/>");
            e.preventDefault();
            e.stopPropagation();
            e.cancelBubble =true;
            return false;
        }
    })



});
$('a').click(function(e){
    content.append('Normal click event<BR/>');
    $(this).trigger('kabhoom');
    e.preventDefault();
    return false;
    
})
// wrapping in ready because otherwise the order is messed up that the event listeners are registered.
$(document).ready(function(){
 $('a').on( 'kabhoom' , function(e){
    // Are we good to go? Or has it stopped?
    if(!e.isPropagationStopped()) {
        content.append('Should not appear<BR/>');
    }
  });
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="frut">
</div>
<a href="https://www.google.com">KHaboom?</a>

Edit 2

Your problem comes down to this:

Your khaboom event has NO way to stop the click propagation. The khaboom is triggered as a separate process by the trigger command.

It gets it's own event stream, but has no relation to the click event that triggered it. So it can cancel itself, it can prevent whatever you define should or should not happen, but it cannot touch the click event because there is no reference to it, nor does the click event wait for it.

You can see it as two streams diverging but never touching again. Where one stream ends in the ocean, the other stream runs dead in the mountains(the ocean being navigate wherever). Whilst the other stream in the mountains just does it's thing with the mountains(whatever you decide should happen on its khaboom event).

Visualization:

                 CLICK
                   ||
                   ||
                   \/
                  EVENT
                   ||
                   ||
                   \/
        jQuery.on() call all clicks
                   ||
                   ||
                   \/
         click found trigger khaboom=========== <-- absolute seperation. no more link between two threads.
                   ||                         ||
                   ||                         ||
                   \/                         \/
            no more click events            EVENT
                   ||                         ||
                   ||                         ||
                   \/                         \/
         has preventDefault been called?   jQuery.on() call all khaboom
                   ||                         ||
                   ||                         ||
                   \/                         \/
           No?  Navigate to page defined.    found khaboom. execute
                                              ||
                                              ||
                                              \/
                                             has preventDefault been called?
                                              ||
                                              ||
                                              \/
                                              yes? do nothing because we don't have an end.
Robert
  • 1,286
  • 1
  • 17
  • 37
Tschallacka
  • 27,901
  • 14
  • 88
  • 133
  • check my 1st snippet of Jquery , `return false ` does't execute , and does't get reached probably , and my question is why ? to make it simpler , when the user clicks the link , he is still able to go to the link , did you execute the code ? – Tenali Raman May 27 '15 at 10:43
  • OMG !!! You totally mis understood my question LOOL , just execute my 1st bit of Jquery and u'll se that neither return false nor preventDefault fires ! – Tenali Raman May 27 '15 at 10:46
  • This answer is so so wrong ! you have not bothered to understand my question , let alone answer it , please delete this answer or re-read my question ! I appreciate you trying to help me , but 1st please be sure of the fact that you know what I am talking abt ! – Tenali Raman May 27 '15 at 10:54
  • You learn to code instead of copypasting a couple of lines without knowing program flow. I updated my answer. – Tschallacka May 27 '15 at 11:03
  • check this fiddle out http://jsfiddle.net/qzoz3bj0/3/ and tell me WHY return false does't work , Update your answer ! ... THATS my only question ! ... also , I am not interested in making my code work , I am interested in why is does't work . – Tenali Raman May 27 '15 at 11:07
  • Because you are an idiot. honestly. `Uncaught ReferenceError: $ is not defined`... if you make a jquery fiddle select bloody jquery in the dropdown. http://jsfiddle.net/mdibbets/qzoz3bj0/5/ and return false doesn't work because you need to use `e.preventDefault();` Thats enough. You can use all the other methods as I provided in the snippet. Now please, learn something useful. – Tschallacka May 27 '15 at 11:12
  • thanks for the compliment ! Still does't work http://jsfiddle.net/qzoz3bj0/7/ , preventDefault() does't work too ! – Tenali Raman May 27 '15 at 11:15
  • because you removed the prevent default from the clickhandler. thats the one that decides if the click should navigate or not. You are manually triggering the Khaboom, with no callback whatsoever tot he click function. not using any closure or anything. The click event does not know it gets cancelled. – Tschallacka May 27 '15 at 11:17
  • Well that was my only question ... and I wanted somebody to elaborate why . – Tenali Raman May 27 '15 at 11:19
  • 1
    Next time be more polite, it really helps. – Tschallacka May 27 '15 at 11:19
  • thanks for your effort , BUT all I want to know is why return false does't trigger in this code http://jsfiddle.net/qzoz3bj0/7/ , and I am still not close to getting my answer ! :) – Tenali Raman May 27 '15 at 11:22
  • added second edit. Please take a look. maybe now you'll understand. – Tschallacka May 27 '15 at 11:54