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

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs

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, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint