Ad

JS - Difference Between $("#elementid") Vs Document.getElementById?

- 1 answer

What are the differences, if any, between $("#elementID") vs document.getElementById("elementID")?
Can they both do the same things?

What are these things called? Objects?
i.e.
if I have x = $("#elementID"), what is x?
if I have y = document.getElementById("elementID"), what is y?

Ad

Answer

document.getElementById("elementID") is a call of the DOM function exposed by JavaScript for obtaining a reference to an element in your DOM.

$("#elementID") is a function call to the jQuery JavaScript library that does much the same thing, though the result is a jQuery object encapsulating said DOM element. I'm not going to go into detail about what that means: you can read the jQuery documentation to find out all about that.

Ad
source: stackoverflow.com
Ad