$('#container').on('click', 'a',function()..
and
$('#container a').on('click',function()..
What is the difference between both selectors.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The first will bind to all current and future anchor tags while the second will only bind to existing anchor tags.
the first is almost identical to using
.delegate(), the only difference is the event and selector are swapped.Edit: Answer to comment since it is rather long
The first is useful for dynamic content or content that contains a lot of anchor tags. The first only binds a single event that listens for events to bubble up to it that were triggered on elements that match the given selector. The first one could be re-written as
$("#container").delegate("a","click",function()...for the exact same functionality. It is far more efficient in most cases than binding events directly to the elements. However, if your content is not dynamic and doesn’t contain very many anchor tags, you should use the second piece of code because it is less complex than the first.