Drop-down box. Objectives Learn the HTML syntax of a drop-down list javascript properties of a...

7
Drop-down box

description

HTML syntax option 1 option 2

Transcript of Drop-down box. Objectives Learn the HTML syntax of a drop-down list javascript properties of a...

Page 1: Drop-down box. Objectives Learn the HTML syntax of a drop-down list javascript properties of a list/menu: length options selectedIndex The location sub-object.

Drop-down box

Page 2: Drop-down box. Objectives Learn the HTML syntax of a drop-down list javascript properties of a list/menu: length options selectedIndex The location sub-object.

Objectives• Learn the HTML syntax of a drop-down list

• javascript properties of a list/menu:• length• options• selectedIndex

• The location sub-object of document

• The href property of location

Page 3: Drop-down box. Objectives Learn the HTML syntax of a drop-down list javascript properties of a list/menu: length options selectedIndex The location sub-object.

HTML syntax

<form name="form1"> <select name="menu"> <option value="1">option 1</option> <option value="2">option 2</option> </select></form>

Page 4: Drop-down box. Objectives Learn the HTML syntax of a drop-down list javascript properties of a list/menu: length options selectedIndex The location sub-object.

Javascript properties

To access the select element:document.form1.menu

Properties of the element:lengthoptions (an array)

options[n].valueoptions[n].text

selectedIndex

Event-handlers:onchange / onfocus /onblur

Page 5: Drop-down box. Objectives Learn the HTML syntax of a drop-down list javascript properties of a list/menu: length options selectedIndex The location sub-object.

Example

<form name="form1"> Links <select name="links" onchange=“go()"> <option value=“http://www.google.com">Google</option> <option value=“http://www.facebook.com">Facebook</option> </select></form>

Page 6: Drop-down box. Objectives Learn the HTML syntax of a drop-down list javascript properties of a list/menu: length options selectedIndex The location sub-object.

Example

<script type="text/javascript"> function go(){ var n = document.form1.links.selectedIndex; var url = document.form1.links.options[n].value; window.document.location.href = url; }</script>

Page 7: Drop-down box. Objectives Learn the HTML syntax of a drop-down list javascript properties of a list/menu: length options selectedIndex The location sub-object.

Assignment• Modify the example by targeting the value of the options to

the src attribute of an iframe. (5 points)

• The html code for the iframe is: <iframe src="xx" width="100%" height="400" />