1

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?

agent provocateur
  • 824
  • 3
  • 19
  • 39

2 Answers2

3

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.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

$("#elementID") is a JQuery method that requires you to include JQuery.js on your page and the other is a native browser method.

Using $("#elementID") you can use JQuery to manipulate it. document.getElementById gives you the DOM element

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Ajwhiteway
  • 986
  • 1
  • 9
  • 23