Why does the .on event handler should go inside $(document).ready ?
It works fine even if it is outsite $(document).ready. Why should we put it inside it anyway? As mentioned the .item elements are not present as the ducument is ready.
Answer 55e49bfe51b88742c60004be
First read difference between onclick vs click site:stackoverflow.com
and then why onclick inside document ready site:stackoverflow.com
event- bubbling or Capturing event bubble-up or trickle-down http://stackoverflow.com/questions/4616694/what-is-event-bubbling-and-capturing
Answer 55e49c9086f5525d0d00051a
++ general search Did you try… http://www.codecademy.com/guidance/choose
google search == the Book == jquery [your question] site:developer.mozilla.org CSS [your question] site:developer.mozilla.org javascript [your question] site:developer.mozilla.org [your question] site:jquery.com [your question] site:getbootstrap.com
== discussions / opinions == jquery [your question] site:stackoverflow.com CSS [your question] site:stackoverflow.com javascript [your question] site:stackoverflow.com
== guidance == www.crockford.com [your question] site:crockford.com
http://stackoverflow.com/questions/1795438/load-and-execution-sequence-of-a-web-page https://learn.jquery.com/events/event-delegation/ event- bubbling or Capturing event bubble-up or trickle-down http://stackoverflow.com/questions/4616694/what-is-event-bubbling-and-capturing https://www.codecademy.com/articles/http-errors-404 https://www.codecademy.com/articles
https://developer.mozilla.org/en-US/Learn/HTML/HTML_tags https://developer.mozilla.org/en-US/docs/Web/CSS/Reference https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes http://www.w3schools.com/jquery/jquery_ref_selectors.asp
Answer 55e4a12295e3780e0c00063d
== guidance == www.crockford.com http://javascript.crockford.com/code.html http://javascript.crockford.com/survey.html https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript www.developer.mozilla.org/en-US/docs/Web/JavaScript/Guide
Code-Year https://www.codecademy.com/en/tracks/code-year?jump_to=4fce4211279bae0003006265
teach yourself javascript site:codecademy.com https://www.codecademy.com/en/tracks/teachyourself Meet JSON https://www.codecademy.com/courses/javascript-beginner-en-xTAfX/0/1?curriculum_id=50802b7d5225bf0200000767 Adding Event Handlers The Simple Way https://www.codecademy.com/courses/web-beginner-en-A0uwI/0/1?curriculum_id=50802b7d5225bf0200000767 JavaScript and Cookies https://www.codecademy.com/courses/javascript-beginner-en-5knNv/0/1?curriculum_id=50802b7d5225bf0200000767 Dealing with the DOM https://www.codecademy.com/courses/javascript-beginner-en-gwcYv/0/1?curriculum_id=50802b7d5225bf0200000767 JavaScript and CSS https://www.codecademy.com/courses/web-beginner-en-SEgtM/0/1?curriculum_id=50802b7d5225bf0200000767
Yourself Python: http://www.codecademy.com/courses/python-beginner-en-fymF4
Answer 55e4e8fb937676b81200059f
It actually doesn’t matter if the handler is in the ready handler function body or not. I just passed exercise 13/14 with this:
$(document).ready(function () {
$('#button').click(function () {
var toAdd = $('input[name=checkListItem]').val();
$('.list').append('<div class="item">' + toAdd + '</div>');
});
});
$(document).on('click', '.item', function () {
$(this).remove();
});
and rightly so. The event listener binding is to the document, which is already present at load time so there is no need to defer it. No elements need to be loaded when this listener is attached.
2 comments
I know it works, I wrote it in my question. The exercise says: “ Complete your program by adding the .on() event handler as described above. ******It should go inside your $(document).ready() ******, but after and outside your $(‘#button’).click().” My question is why should it go there as written in the instructions?
It is just the Course-creator whom wanted you to do it this way. As @Roy pointed out there are other ways…
Popular free courses
- Free course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner Friendly4 Lessons - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly11 Lessons - Free course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner Friendly6 Lessons