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

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?

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.


People who read this tip also read these tips:

Look for similar tips by subject:

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business