Is it safe to assume that getElementById works in every browser? If not, which ones do / do not support it?
Is it safe to assume that getElementById works in every browser? If not, which
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
All browsers support this however one issue I came upon in IE6&7 (adding to @Esailija’s answer)
Do This:
Don’t Do This:
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: