Tuesday, January 29, 2019

javascript - document.getElementById vs jQuery $()



Is this:



var contents = document.getElementById('contents');



The same as this:



var contents = $('#contents');


Given that jQuery is loaded?


Answer



Not exactly!!




document.getElementById('contents'); //returns a HTML DOM Object

var contents = $('#contents'); //returns a jQuery Object





In jQuery, to get the same result as document.getElementById, you can access the jQuery Object and get the first element in the object (Remember JavaScript objects act similar to associative arrays).



var contents = $('#contents')[0]; //returns a HTML DOM Object


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...