Can I Use The Id Of An Html Element As A Variable In Javascript?
Solution 1:
In the early days of browser scripting, IE made ID and NAME attribute values into properties of the global object that referenced the related elements. That was widely considered a "bad thing", but was copied by most other browsers in order to be compatible with IE (most sites at the time were written almost exclusively for IE, which had about 95% user share).
Then came open standards and a concerted effort to support them. Now no one with any sense uses it, though it's still supported by probably all browsers in use.
Note that declared global variables of the same name take precedence over a same–named or ID'd element.
Solution 2:
I'm going to refer you to this answer and mention that this is notstandard behaviour. This behaviour is supported by all browsers (sans Firefox outside of quircks mode) but I would not recommend its use. This is not supported by firefox<14 in standards mode
Solution 3:
You can rely on it. But if the language running the site is ASP.NET, it is safer to use the ClientID. If the ID of the control changes, your code will adapt.
e.g. For Javascript running over ASP.NET
var myCanvas = document.getElementById("<%=myCanvas.ClientID%>");
Post a Comment for "Can I Use The Id Of An Html Element As A Variable In Javascript?"