Last Updated: February 25, 2016
·
1.813K
· nashby

Accessing DOM node by id

If you have DOM node with id like

<div id="foo"></div>

you can access it by referring to the variable named according to the id of DOM node:

alert(foo)

You can test it using Developer Tools in Chrome:

3 Responses
Add your response

Doesn't that mean ID-used words are reversed now (can't be used as variable names)?

over 1 year ago ·

Hey, you can read detailed explanation here http://stackoverflow.com/a/3434388/336626

over 1 year ago ·

too much magic, can be clobbered by variables named the same as an element ID

over 1 year ago ·

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside