December 4, 2000 - Using Tokens in Element Names | WebReference

December 4, 2000 - Using Tokens in Element Names

Yehuda Shiran December 4, 2000
Using Tokens in Element Names
Tips: December 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

JavaScript is very sensitive to its language tokens, so be careful not to use any token in element names. The following script works fine in Netscape 6 and Internet Explorer:

<FORM NAME="election">
<SELECT NAME="presidents">
<OPTION NAME="leaving">Al Gore
<OPTION SELECTED>George W. Bush
</SELECT>   
<INPUT TYPE="button" VALUE="Switch first option" onclick="document.election.presidents.item(0).text =
 'Bill Clinton'">
</FORM>

Click the button below and see how the first option is changing on the fly:

Now let's change the name of the SELECT element above from NAME="presidents" to NAME="new". The script should look like that now:

<FORM NAME="election">
<SELECT NAME="new">
<OPTION NAME="leaving">Al Gore
<OPTION SELECTED>George W. Bush
</SELECT>   
<INPUT TYPE="button" VALUE="Switch first option" onclick="document.election.new.item(0).text =
 'Bill Clinton'">
</FORM>

But the token new cannot be used that casually. The JavaScript interpreter cannot handle it as a name, and will complaint to you. Internet Explorer will show a warning icon in the status bar. Double-click it to see a new window with a cryptic message. Your defaults may be set to always display the error window. In Netscape 6, you need to go to the Task/Tools/JavaScript Console and see the errors there. Try it now.