Thursday, January 28, 2010

getElementById

Firefox doesnot recognize "name" attribute

This is to expose how Internet Explorer and Firefox browsers treat the getElementById method. This method takes the id of the field (normally the 'INPUT' field) as paramenter. If you give the id using id attribute it will work in both Internet explorer and Mozilla firefox. If you use 'name' attribute to give name to your input field/tag it won't be recognized by Firefox and your JavaScript won't work with getElementById.


Here is the example code: This will work in both IE and FireFox
Type something



In the above example i use a text filed and set its id property to 'YourName". I used a button and called a simple JavaScript function in it OnClick event to display the text in the textbox. I used the alert function to display the message box. This will work both in IE and Firefox. In the below example i set the name attribute to 'YourNameIE'. Note, i used 'name' attribute instead of 'id' attribute. That is the difference. This won't work in Firefox when you click the Click me button. That's it. So don't use name attribute in field naming.

This will work only in Internet Explorer
Type something

No comments:

Post a Comment