0

Sometimes I need to get elements like this:

var object = document.getElementById('ObjectName');

And sometimes like this:

var object = document.getElementById('#ObjectName');

What’s the difference between them?

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
Moondustt
  • 864
  • 1
  • 11
  • 30
  • [Related](https://stackoverflow.com/search?tab=votes&q=%5bjs%5d%20getelementbyid%20code:"%23"): [Why does `document.getElementById("#datepicker1")` not find my element?](https://stackoverflow.com/q/33036800/4642212), [Why am I getting “Cannot set property 'innerHTML' of null at ahint”](https://stackoverflow.com/q/54554975/4642212), [Why does `document.getElementById("#sideNav")` not find my element?](https://stackoverflow.com/q/38479889/4642212), [\[1\]](https://stackoverflow.com/q/62478560/4642212), [\[2\]](https://stackoverflow.com/q/17303764/4642212), and probably a few more. – Sebastian Simon Jul 06 '20 at 05:02

4 Answers4

8

No, you don't see

var object = document.getElementById('#ObjectName');

You don't see that because that would mean the id of the element starts with # and HTML4 id could only start "with a letter ([A-Za-z])".

What you see is sometimes people using the jQuery library in which the query language allows you to find an object using

var elem = $('#objectId');

And in the future you'll see more and more people using a similar query language with querySelector or querySelectorAll.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • 2
    But `id` attributes starting with `#` are fair game in HTML5, so the questioner technically *could* have seen that code. – Frédéric Hamidi Mar 18 '13 at 20:27
  • 1
    @FrédéricHamidi "I always used it" lets me think OP isn't dealing with code gaining from the last versions of HTML. – Denys Séguret Mar 18 '13 at 20:28
  • This answer is a bit out of date (HTML5 allows `#` at the beginning, even though it’s recommended to start with a letter, and it is the future now). The other answers are more up to date. At least fix the unmatching parenthesis. – Sebastian Simon Jul 06 '20 at 12:30
7

The # is part of the ID selector in CSS. There are libraries and methods which support CSS selectors for selecting elements, such as jQuery and the native DOM methods querySelector and querySelectorAll.

But in "traditional" DOM method, the # has no special meaning. getElementById('#ObjectName') would select an element which literally has the ID #ObjectName, i.e. <div id="#ObjectName">.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
6

The former gets an element with id="ObjectName". The latter gets an element with id="#ObjectName".

In the context of a Selector (as used, for example, by CSS and document.querySelector but not with getElementById) the # character indicates that the text following it is an id.

document.querySelector('ObjectName') (using a type selector) would get <ObjectName /> (not valid HTML) while document.querySelector('#ObjectName') would get an element with id="ObjectName".

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
-1
'#ObjectName'

JQuery (EDIT: or other JavaScript Libraries)

'ObjectName'

DOM (getElementById)