10 Answers
New AnswerIf you name an element in your HTML document using the id attribute, and if the Window object does not already have a property by that name then the id is created by default as property of global object , and the Window object is given a property whose name is the value of the id . That's the reason why you can use the id without using getElementById
IDs of elements are registered as global variable. Some say this was Chrome specific, so I'm not too sure whether all browsers shows such behaviour. https://dev.to/vuelancer/dom-element-id-as-global-variable-1l2n https://2ality.com/2012/08/ids-are-global.html
Cuz you have used onclick https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onclick https://www.w3schools.com/jsref/event_onclick.asp
Even i delete onclick event and use set interval to change the divs text i still dont need to get element by id in js and can directly use the id in js🤔
Shubham Yadav oh interesting, so that's the reason why it gives error when i try to get the id in js and store it in the same name as the id in html
1st write <input id='name' type="text"> in HTML & then use getElementById('name'); in js to get the id which you specified in your html code
Sololearn Inc.
535 Mission Street, Suite 1591Send us a message