0

I found this line of code and i don't know what it does:

$("body").on("click", "#my_info_box, a.submitSomething", function(){
            doSomething();
        });
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
user2520410
  • 459
  • 1
  • 5
  • 12
  • 2
    It's called [event delegation](https://learn.jquery.com/events/event-delegation/), you can do more research on this topic. – Felix Feb 16 '14 at 16:40

4 Answers4

0

A delegated-events approach attaches an event handler to only one element, the tbody, and the event only needs to bubble up one level (from the clicked tr to tbody):

$( "#dataTable tbody" ).on( "click", "tr", function() {
  alert( $( this ).text() );
});

More found here: http://api.jquery.com/on/

Paul Way
  • 1,966
  • 1
  • 13
  • 10
0

It does event delegation. It means that jQuery will listen for changes inside the #my_info_box box that might happen with AJAX and will still work on the new data. Otherwise it cannot know new data was loaded with AJAX.

Banago
  • 1,350
  • 13
  • 22
0
  1. First parameter is for event for track.
  2. Second is for additional selector(Event delegation)
  3. Third is for callback function.

So simply: .on() is an alternative for .click(). But it works also for dynamically created elements.

aksu
  • 5,221
  • 5
  • 24
  • 39
0

Sets an event handler for clicking the element with #my_info_box and elements having .submitSomething. The handler is the function() { doSomething(); }

aksu
  • 5,221
  • 5
  • 24
  • 39
Kostas
  • 1,903
  • 1
  • 16
  • 22