I was trying to grab the 'value' of a form input with document.getElementById and noticed something strange.
This worked fine:
This, however, would not work:
var grab = document.getElementById("name");
grab.value = "enter name here";
The problem seems to be that if the variable has the same name as the ID, it won't work. Does anyone know why this is? I looked in my JS reference book and on MDN but could not find the answer.
var name = document.getElementById("name");
name.value = "enter name here";
to make a variable restricted to a function use this.name. You can also declare global variables within a function with window.name - "name" being the variable name.