Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - Select element by id name [duplicate]

Tags:

javascript

Normally I select elements with an id via document.getElementById('idName'). But apparently you can also select them by name only 'idName'. I was not aware of this until recently. I wanted to know if this is a "bad practice" to select elements by their idName? I have already used the search engine and found nothing about select by id name. So here is my question now.

enter image description here

 console.log(idName)
<div id="idName">Hello World</div>
like image 896
Maik Lowrey Avatar asked Jul 03 '26 16:07

Maik Lowrey


2 Answers

I believe this is a bad practice because you can get yourself confused and cause conflicts with your own js variables.

like image 159
César O. Araújo Avatar answered Jul 05 '26 06:07

César O. Araújo


I can't say it's Good or bad But

you should be very careful while using . This can cause many problems So I will recommend use in Small Projects only . (it's My personal recommendation )

Suppose Your JavaScript loads First then it will throw an error and none of the code will work

For more Information

just typing in 'idName' isn’t a “short form” of document.getElementById() It’s a reference to a global variable


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!