What is the difference between the document selector and window selector?

JZ. picture JZ. · May 26, 2011 · Viewed 36.4k times · Source

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);
      }
  });

Answer

Itai Sagi picture Itai Sagi · May 26, 2011

$(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).