Is there a way to use one() with event delegation? like on() or live() so that it will be applied to DOM elements that are not created at the binding time?
Is there a way to use one() with event delegation? like on() or live()
Share
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.
Yes. From the jQuery
.one()docs:This means that, when you use the form
$(el).one('click', '.selector', handler)it will work likelive— the event handler is bound toel(which has to exist in the DOM) but the event handler will only be executed on clicks on.selector, regardless of whether that element exists during your call to.one().Edit: It seems that jQuery’s documentation on this is wrong — at least, the way I interpret it. According to the
alertabove,Which makes me think that if anything else is clicked in the body first, the event handler will be removed and won’t fire again. This is not the case — the event handler will exist until
#foois clicked one time.See this fiddle for a demonstration.