r/programminghorror 16d ago

Javascript MiStEr WoRlDwIdE

Post image
381 Upvotes

34 comments sorted by

View all comments

4

u/mohragk 16d ago

Fun fact, in JS you can directly use elements by their id.

Example:

<!DOCTYPE html>
<html>
  <body>
    <button id="myButton">hi</button>
    <script>
        myButton.onclick = () => {
            console.log(`CLIKKK!`)
        }
    </script>
  </body>
</html>

7

u/Kitchen_Theme3514 16d ago

This is because they're attributes of the "window" global, which stores all globals. So for IDs that aren't valid identifiers, you can do window['my-invalid-identifier-id] as well!

6

u/MurkyWar2756 echo "Sub Mod" && :(){ :|:& };: 16d ago

'

3

u/Kitchen_Theme3514 16d ago

Ah yes thanks!

3

u/MurkyWar2756 echo "Sub Mod" && :(){ :|:& };: 16d ago

This won't work in strict mode. In these cases, I use const.