Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listen to all onClick events of all children in jQuery

I can listen to all mrow click events of a div, using sometihng like

$('#mydiv').on('click', 'mrow', function() {
    var moo = $(this).attr('id');
    if (handlers[id]) {
        event.stopPropagation();
        handlers[id]();
}

Can I use a similar setup to listen on all click events of all the children of the div (Without setting up, separate listeners for every type)? Some part of the tree have handler functions and some don't, and I want these requests to propagate up within the div, until one that has a handler is found.

like image 363
Guest 86 Avatar asked May 27 '13 13:05

Guest 86


3 Answers

$('#mydiv').on('click', '*', function() {

* will bind to all element's within #mydiv

Demo --> http://jsfiddle.net/Vjwqz/1/

like image 136
Mohammad Adil Avatar answered Oct 15 '22 21:10

Mohammad Adil


For all descendants:

$('#mydiv').on('click', '*', function() {...});

For direct descendants: {what is called children in javascript}

$('#mydiv').on('click', '> *', function() {...});
like image 15
A. Wolff Avatar answered Oct 15 '22 21:10

A. Wolff


I think you can leave the selector

$('#mydiv').on('click', function() {

selector Type: String A selector string to filter the descendants of the selected elements that trigger the event. If the selector is null or omitted, the event is always triggered when it reaches the selected element.

http://api.jquery.com/on/

like image 2
Ronald Avatar answered Oct 15 '22 23:10

Ronald