4

I just noticed, its not necessary anymore in Chrome to select elements that have an id e.g with getElementById, you can directly call that element with its id name... e.g "para1" in this example:

<head>
  <title>getElementById Beispiel</title>
  <script>
    function changeColor(newColor) {
      para1.style.color = newColor;
    }
  </script>
</head>

<body>
  <p id="para1">Irgendein Text</p>
  <button onclick="changeColor('blue');">Blau</button>
  <button onclick="changeColor('red');">Rot</button>
</body>

is there any more specification on this? didn't find anything about it on the internet..

adiga
  • 34,372
  • 9
  • 61
  • 83
MMMM
  • 3,320
  • 8
  • 43
  • 80
  • 1
    Internet Explorer introduced the notion of using "id" attribute values to implicitly create window properties way back in the 90s. It was a terrible idea then and it's a terrible idea now. – Pointy Jun 26 '20 at 12:32
  • Why was it terrible? – rfii Jan 15 '21 at 18:04
  • @rfii The idea is bad because it doesn't always work. The `window` object has many top-level properties and methods. You can create an element with the ID of `exampleID` and reference it globally, but IDs like `alert` or `console` will not function correctly because these are the names of already available methods on the window object. Another issue is namespace pollution as all the unique IDs create global variables and you'd generally want to reference elements using some getter method like `getElementById` locally inside of a function's scope where there is no chance of it being overwritten – Shape May 31 '23 at 16:27

0 Answers0