Profile image of objectRunner87862
Submitted by objectRunner87862
over 9 years

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

0 votes

Permalink

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

Profile image of leonhard.wettengmx.net
over 9 years

Answer 55e49c9086f5525d0d00051a

0 votes

Permalink

++ 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

Profile image of leonhard.wettengmx.net
over 9 years

Answer 55e4e8fb937676b81200059f

0 votes

Permalink

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.

Profile image of mtf
Submitted by mtf
over 9 years

2 comments

Profile image of objectRunner87862
Submitted by objectRunner87862
over 9 years

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?

Profile image of leonhard.wettengmx.net
over 9 years

It is just the Course-creator whom wanted you to do it this way. As @Roy pointed out there are other ways…

Answer 5613b6ad3e0ec8b13b00028d

0 votes

Permalink

I did it like this and it passed

$(document).on(‘click’,’.item’,function(){ $(‘#button’).click(function(){ var toAdd=$(‘input[name=checkListItem]’).val(); $(‘.list’).append(‘

‘ + toAdd + ‘
‘); $(this).remove(); }); });

Profile image of Vitnere
Submitted by Vitnere
over 9 years