mattsanchez — 2012-11-26T17:26:06-05:00 — #1
I was trying to grab the 'value' of a form input with document.getElementById and noticed something strange.
This worked fine:
var grab = document.getElementById("name");
grab.value = "enter name here";
This, however, would not work:
var name = document.getElementById("name");
name.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.
felgall — 2012-11-26T20:15:53-05:00 — #2
wazzaday — 2012-11-27T07:27:29-05:00 — #3
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.