Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What browsers support getElementById?

Is it safe to assume that getElementById works in every browser? If not, which ones do / do not support it?

like image 579
KingKongFrog Avatar asked Dec 11 '12 23:12

KingKongFrog


Video Answer


1 Answers

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);  
like image 86
KingKongFrog Avatar answered Sep 19 '22 16:09

KingKongFrog