Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript jQuery Basics (2014) Creating a Simple Drawing Application Perform: Part 3

Ronnelle Torres
Ronnelle Torres
5,958 Points

Why can't we use $(".controls li").on("click", function(){}) ?

Why can't we use $(".controls li").on("click", function(){}) ? I'm assuming that it's the same as $(".controls").on("click", "li", function(){});

On the API documentation, they used an example:

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

Please enlighten :)

2 Answers

Hey Ronnelle, if you look up the on() method in the documentation you will find that it has replaced the deprecated method delegate(), this means that they relatively do the same thing. So, basically they take the same arguments. If you look that up, you will find that you can use multiple events with the on() method, among with other specific arguments. so the main difference between on and click is that with .click() you are restricted to one event- the click event, but with .on() you can use multiple events and more importantly, it makes it easier in the future to change the event if you decided so.

I hope this helps! ^^

Hi Ronnelle,

To be honest I would just be using the .click( function() {}); syntax for click events.

Is this part of the lesson?


Ronnelle Torres
Ronnelle Torres
5,958 Points

Yes. it's part of the lesson. It says $(".controls li").click(function(){}) will only bind on existing li inside the .controls. While using $(".controls").on("click", "li", function(){}); will allow us to bind the listener "click" on elements that is already existing inside the DOM and dynamically added elements.