W3C home > Mailing lists > Public > www-html@w3.org > October 2012

Syntax to access div by ID.

From: Gautam Sathe <gautamsathe@justdial.com>
Date: Thu, 11 Oct 2012 17:35:25 +0530
Message-ID: <5076B605.8030701@justdial.com>
To: www-html@w3.org
Bit of a preamble:

Just got out of a really long argument with one of the developers 
working for me about what I perceived as a syntax error... we made a 
test page to settle the argument and I wound up feeling like I have no 
clue about JS/Web development in general.

So here's what we argued about:

He has some HTML in a page which looks a bit like this:

<div id="testDiv">...</div>

In a script section he was trying to set its display property to 'none' 
and he had coded it like:

testDiv.style.display = 'none';

This is a simplification of the actual code- but works for the sake of 
testing/demonstrating what actually happened.

I argued and told him that the syntax is incorrect, that since the ID is 
an HTML entity he cannot reference it directly in JS and that this would 
generate errors. I told him to use either
    document.getElementById('testDiv').style.display='none'
or else (since we are using jQuery)
    $('#testDiv').hide()

Well, to cut a long story short - he argued back in defense of his code 
and on testing we found that his syntax just "works" - tried Chrome, 
Firefox... this has me really confused.

Has this always worked? Is it supposed to work this way? Are there any 
drawbacks to this approach? Does anyone know of any current OS/Browser 
combination where this would NOT work?

I can see a lot of our libraries shrinking a bit in size if we can 
eliminate the needless syntax.
-- 
Confused

Disclaimer

  The information contained in this communication is intended solely for the
  use of the individual or entity to whom it is addressed and others
  authorized to receive it. It may contain confidential or legally
  privileged information. If you are not the intended recipient you are
  hereby notified that any disclosure, copying, distribution or taking any
  action in reliance on the contents of this information is strictly
  prohibited and may be unlawful. If you have received this communication
  in error, please notify us immediately by responding to this email and
  then delete it from your system. The company is neither liable for the
  proper and complete transmission of the information contained in this
  communication nor for any delay in its receipt.
Received on Friday, 12 October 2012 12:19:48 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Friday, 12 October 2012 12:19:49 GMT