Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript $(document).on() vs $('#id').on()

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #167866
    gowda24
    Participant

    Whats the difference between

    $(document).on(‘click’,’#id’, function(){
    //code..
    });

    $(‘#id’).on(‘click’, function(){
    //code..
    });

    #167867
    dyr
    Participant

    In the first case you’re attaching the event listener to the document, which listens for an event originating from an element with a particular id that bubbles up the DOM.

    In the second case, you’re attaching the event listener to the element itself.

    Go here: https://api.jquery.com/on/ and read the section with the heading “Direct and delegated events.”

    Cheers :)

Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.