spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / javascript / beginning / chap6 / 5 11345
[previous] [next]
Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs

Beginning JavaScript

Try It Out – Adding and Removing List Options

Let's use the 'list of days' example we saw above to demonstrate adding and removing list options.

<HTML> <HEAD> <SCRIPT LANGUAGE=JavaScript> function butRemoveWed_onclick() { if (document.form1.theDay.options[2].text == "Wednesday") { document.form1.theDay.options[2] = null; } else { alert('There is no Wednesday here!'); } } function butAddWed_onclick() { if (document.form1.theDay.options[2].text != "Wednesday") { var indexCounter; var days = document.form1.theDay; var lastoption = new Option(); days.options[6] = lastoption; for (indexCounter = 6;indexCounter > 2; indexCounter--) { days.options[indexCounter].text = days.options[indexCounter - 1].text; days.options[indexCounter].value = days.options[indexCounter - 1].value; } var option = new Option("Wednesday",2); days.options[2] = option; } else { alert('Do you want to have TWO Wednesdays?????'); } } </SCRIPT> </HEAD> <BODY> <FORM NAME=form1> <SELECT NAME=theDay SIZE=5> <OPTION VALUE=0 SELECTED>Monday <OPTION VALUE=1>Tuesday <OPTION VALUE=2>Wednesday <OPTION VALUE=3>Thursday <OPTION VALUE=4>Friday <OPTION VALUE=5>Saturday <OPTION VALUE=6>Sunday </SELECT> <BR> <INPUT TYPE="button" VALUE="Remove Wednesday" NAME=butRemoveWed onclick="butRemoveWed_onclick()"> <INPUT TYPE="button" VALUE="Add Wednesday" NAME=butAddWed onclick="butAddWed_onclick()"> <BR> </FORM> </BODY> </HTML>

Save this as ch6_examp7.htm.

If you type the page in and load it into your browser, you should see the form below. Click the Remove Wednesday button and you'll see it disappear from the list. Add it back by clicking the Add Wednesday button. If you try and add a second Wednesday or remove a non-existent Wednesday, then you'll get a polite warning telling you that you can't do that.

browser1

home / programming / javascript / beginning / chap6 / 5 11345
[previous] [next]

Copyright 1999 Wrox Press Ltd. and

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


Created: February 7, 2001
Revised: February 7, 2001


URL: http://webreference.com/programming/javascript/beginning/chap6/