www.webdeveloper.com

View Poll Results: Did you like it?

Voters
2. You may not vote on this poll
  • Yes

    1 50.00%
  • No

    1 50.00%
  • No Comments

    0 0%
Multiple Choice Poll.
Results 1 to 7 of 7

Thread: New Way of Accessing DOM

  1. #1
    Join Date
    May 2012
    Posts
    3

    Thumbs up New Way of Accessing DOM

    Hi All

    Suprisingly it was new for me, when i was working with a core javascript code and found something very interesting there.

    It was a new way DOM elements are being processed in javascript. I am not sure whether this is pure luck or something really interesting and innovating. Let me help decide this...

    Problem:
    It was a simple textbox like
    Code:
    <input type="text" id="userName" />
    On a button click i just wrote like :
    Code:
    alert(userName.value)
    and surprisingly it worked.

    I am not getting how this is being treated as a DOM object. i debugged and found complete DOM object is available once we use element id as a variable in javascript.

    I am not getting exact reason why it is happening and if possible how it is getting converted as a DOM object, and if its so, so what's the use of javascript/jquery left to make DOM for html elements.

    kindly help me resolve this, wishing for a innovation soon

    Thanks
    Jatin

  2. #2
    Join Date
    Feb 2006
    Posts
    2,927
    This is a holdover from older days, and has a gotcha- if you have any variables with the same name as an id, the variable is exposed and the id is not. Also, the behavior is not in any specification, and is not guaranteed to continue in future versions of browsers that use it now. Better to use getElementById...

  3. #3
    Join Date
    Feb 2011
    Posts
    231
    Hi
    I tested and seems to works, also with other HTML tags::
    Code:
    <div id="userName"><b>test</b></div>
    
    <script type="text/javascript"><!--
    alert(userName.innerHTML);        // <b>test</b>
    --></script>
    - But, for safe, I think it is better to use document.getElementById('id') or with jQuery $('#id') .
    Last edited by MarPlo; 05-15-2012 at 11:23 AM.

  4. #4
    Join Date
    May 2012
    Posts
    3
    Yes Marplo

    You are correct. i tested it at my end and surprisingly it worked for any HTML tag.

    So i understand for precautions its better to use getElementById/Jquery DOM accessor. But was just wondering whatr's the reason/cause for this actually conceptually.

    Even its not the case with IE only it works with any browser, so we can't take it as a constraint of browser only. Any conceptual reason for this behaviour?

    Thanks
    Jatin

  5. #5
    Join Date
    Feb 2011
    Posts
    231
    Hi,
    This is also something new for me too.
    Maybe someone professional know the answer.

    Anyway, in the console of firebug addon in Mozilla appear this warning:
    "Element referenced by ID/NAME in the global scope. Use W3C standard document.getElementById() instead".

  6. #6
    Join Date
    May 2012
    Posts
    3
    Hi Marplo

    Thanks for your help, i think what Firefox says is something which seems to be suprising, it means by default for each element a DOM is already available pre made. Its convention to use getEleementById, but its availability is already there though.

    So was just wondering is this the feature of browser or so, because each browser provides these, so might be a feature available above the limitations of browser.

    So just wondering about some detailed inputs for the reason. Hope so we learn something new

    Thanks
    Jatin

  7. #7
    Join Date
    Aug 2007
    Posts
    3,767
    Quote Originally Posted by jatinkindra View Post
    So just wondering about some detailed inputs for the reason. Hope so we learn something new
    What's your question? As mrhoo said, it's still allowed occasionally for backwards compatibility, other than that, it's entirely wrong and you should never use it. Nothing to be learned.
    Great wit and madness are near allied, and fine a line their bounds divide.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles