7

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>
Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79

2 Answers2

1

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

1

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

  • 1
    I see it the same way you do. I would have no problem using it for smaller projects. But for larger ones, it can lead to problems. – Maik Lowrey Jan 13 '22 at 13:38