Without using document.getElementById

A few weeks ago I was surprised at at the fact that something that seemed impossible was / is still possible.

I didn't realize that this was even possible in my 20+ years of web development. I even had a debate with my colleague about not using it that way but we weren't in sync.

Apparently - if you set in your HTML <input type="text" id="first_name" /> , you don't need to do document.getElementById('first_name').value - instead, you can directly call first_name.value !!!

https://codepen.io/anjanesh/pen/GRLJZrj

Apparently, this is still valid and is also in the HTML spec.

the value of the id content attribute for all HTML elements that have a non-empty id content attribute and are in a document tree with window's associated Document as their root.

0
Subscribe to my newsletter

Read articles from Anjanesh Lekshminarayanan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Anjanesh Lekshminarayanan
Anjanesh Lekshminarayanan

I am a web developer from Navi Mumbai working as a consultant for cloudxchange.io. Mainly dealt with LAMP stack, now into Django and trying to learn Laravel and Google Cloud. TensorFlow in the near future. Founder of nerul.in