document.getElementById vs jQuery $() (DGJ)

Is this:

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

Just like this:

var contents = $('#contents');

Given jQuery is loaded?

It turns out that the method above is very wrong!!, it should be like this:

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

Post a Comment

Previous Next

نموذج الاتصال