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);
}
});
While using the
windowordocumentobject in a jQuery dom selector, most of the time you won’t notice a difference between the two.However, it’s important to note that they are not the same object.
window– refers to the viewport. It’s used as the main global object in JavaScript.document– a direct descendant ofwindow; refers to the root of the document tree.All DOM elements are a descendant of the
document, which is a direct descendant ofwindow.