Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

event.stopPropagation() not working in chrome with jQuery 1.7

For some reason clicking the document isn't working in Chrome (the closeQuickView is not being called).

The elements are loaded via AJAX and so need to have .on() action (previously .live() which is now deprecated in jQuery 1.7)

Used the example given here: How do I detect a click outside an element? as a basis

$('html').on('click', '.poster:not(.active) .image-effect', function (event) {

        var obj = $(this).parent();

        // If there are no .close spans
        if (obj.find('.close').length === 0) {

            // Add the close element by javascript to remain semantic
            obj.find('.quick-view').append('<span class="close">&times;</span>');
        }

        // Close any open Quick Views
        closeQuickView();

        // Add the active class (controls opacity)
        obj.addClass('active');

        // Fade in the Quick View
        obj.find('.quick-view').fadeIn(200, 'easeInOutQuint');

        event.preventDefault();
        event.stopPropagation();

    });

    $('html').on('click', '.active', function () {
        return false;
    });

    $('html').on('click', '.close', function () {
        closeQuickView();
    });

    $('html').on('click', '.quick-view', function (event) {
        event.stopPropagation();
    });

    // Close the QuickView with a button
    $('html').on('click', function () {
        closeQuickView();
    });

    function closeQuickView() {
        $('.poster').removeClass('active');
        $('.quick-view').fadeOut(200, 'easeInOutQuint');
    }

My markup is as follows:

<figure class="grid_2 box poster">
    <a class="image-effect" href="#">
        <img class="colour" src="path/to/image" />
        <img class="bw" src="path/to/image" />
    </a>
    <div class="quick-view">

        Content

    </div>
</figure>
like image 332
Craig Avatar asked Nov 05 '11 17:11

Craig


People also ask

What does event stopPropagation () do?

The event. stopPropagation() method stops the bubbling of an event to parent elements, preventing any parent event handlers from being executed.

What's the difference between event preventDefault () and event stopPropagation () methods?

preventDefault() prevents the default browser behavior for a given element. stopPropagation() stops an event from bubbling or propagating up the DOM tree.

What is stopPropagation vs stopImmediatePropagation?

stopPropagation() allows other handlers on the same element to be executed, while event. stopImmediatePropagation() prevents every event from running.

Should I use stopPropagation?

Stop propagation is needed when you have JavaScript running on the same event of nested elements. Imagine having a click event on a parent element AND a child. If you clicked the child, and don't want it to also count as a click for the parent, then you need to stop propagation in the child click handler.


1 Answers

Try event.stopImmediatePropagation

Refer documentation

like image 170
Sam Avatar answered Oct 29 '22 13:10

Sam