Is it safe to assume that getElementById
works in every browser? If not, which ones do / do not support it?
All browsers support this however one issue I came upon in IE6&7 (adding to @Esailija's answer)
Do This:
<input type="text" name="address" id="address" value="5th Avenue" />
Don’t Do This:
<input type="text" name="full_address" id="address" value="5th Avenue" />
The reason you should do this is because in Internet Explorer, if you’re trying to target an element using getElementById
, for some reason that browser will search the name attribute of certain elements on the page, in addition to the id. Assuming we’ve used the wrong method for coding the name and id values, the code blocks below will get the exact same result in IE7:
var fullAddress = document.getElementById("full_address");
alert(fullAddress.value);
var fullAddress = document.getElementById("address");
alert(fullAddress.value);