2

Recently, when I am working on some javascript code, I found that I can get a html dom by using a variable which has the same name with the html dom's ID property. For example: <input type="hidden" id="foo" value="100"> console.log(foo.value); I can get the value of foo dom. The result is 100. The question is: Is it a syntax sugar from browser(some javascript parser such as V8)? Can I get doms by this way?

Any help will be appreciated!

Alfred
  • 161
  • 10
  • not following you... what is what you are trying to achieve? – Dalorzo Aug 18 '14 at 02:40
  • This same thing works in my Chrome browser, but I wouldn't imagine this is standardized in any way. Don't depend on this method working in all browsers or in the same way. – PitaJ Aug 18 '14 at 02:44
  • Question seems clear and concise. I'm curious about this behaviour too. – Michael Kang Aug 18 '14 at 02:44
  • 1
    Is that code actually true? Or pseudo? Can you paste actual code you have received that for? This doesn't make sense. JS will register forms with names to variables, but not like this – casraf Aug 18 '14 at 02:44
  • @ChenAsraf Yes, it's easy to check: http://jsfiddle.net/AniHouse/hvaxqjvn/ – Spencer Wieczorek Aug 18 '14 at 02:46
  • **There is nothing related to engine its because element ID are a global property , but this is not a correct and safe way although correct way access value via 'this' by passing it as a argument** // Javascript function foo(elem){ var value = elem.value; var id = elem.id; } or by document.getElementById('ids'); :) – Suraj Rawat Aug 18 '14 at 02:51
  • @Dalorzo thanks for your comment, question has been updated – Alfred Aug 18 '14 at 02:52

0 Answers0