Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.