Jquery – the difference between the document selector and window selector

documentjqueryjquery-selectorswindow

I have the following JQuery function that takes user input and displays it on screen. When I select for both $(document) and $(window) the function works. What is the disadvantage to using either selector? Where can I read more on these selectors and their differences?

Thank you in advance.

  $(document).keypress(function(e) {
      if(e.keyCode == 13) {
          var id = $("input#example").val()
          console.log(id);
          $('#data').append(id);
      }
  });

Best Solution

$(window) selector is for selecting the viewport

$(document) selector is for the entire document (that is, what's inside the <html> tag, even if it exapnds beyond the viewport).