Tom Rudge Tom Rudge - 11 months ago 99
Javascript Question

Ignore Parent onClick event when Child element is clicked

Please see -

By clicking any of the div.rows (lets call this parent), an onClick event is activated, for demo purposes an alert pops up.

Within the row an accordion collapses when the "info" (orange button) is clicked (lets call this child), but the problem is that the parents alert triggers.

I need the info button not to trigger the alert, only when anywhere else is clicked the alert appears.

I've tried various ways such as using (e)stopPropagation .on .off calls etc... I'm not the best jquery guy so need a little help getting this to work - and also help me learn something!

<div class="row ticket-selector" onclick="alert('Hello World!');">
<a data-toggle="collapse" data-parent="#ticket-panel" href="#collapseOne" class="tickets-more"><span class="halflings info-sign orange"></span></a>

The above is the jist - but see for better understanding -


The idea is to remove onclick="" handler saving its value in another field and then to execute (evaluate) value in custom click event handler:

Example code.

    var elements = $(".ticket-selector");
        var handler = $(this).attr('onclick');
        $(this).data('click', handler);
    elements.attr('onclick', '');
        if (!$("info-sign"))