Modifying SELECT Options : JavaScript DHTML examples (example source code) » Form Control » Option Select ComboBox

JavaScript DHTML
C++
Java Products
Java Articles
JavaScript DHTML Home  »   Form Control   » [  Option Select ComboBox  ]   
 

Modifying SELECT Options

Please note that some example is only working under IE or Firefox.

/*
JavaScript Bible, Fourth Edition
by Danny Goodman 

Publisher: John Wiley & Sons CopyRight 2001
ISBN: 0764533428

*/


<HTML>
<HEAD>
<TITLE>Changing Options On The Fly</TITLE>
<SCRIPT LANGUAGE="JavaScript">

// flag to reload page for older NNs
var isPreNN6 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion<= 4)
// initialize color list arrays
plainList = new Array(6)
hardList = new Array(6)
plainList[0"cyan"
hardList[0"#00FFFF"
plainList[1"magenta"
hardList[1"#FF00FF"
plainList[2"yellow"
hardList[2"#FFFF00"
plainList[3"lightgoldenrodyellow"
hardList[3"#FAFAD2"
plainList[4"salmon"
hardList[4"#FA8072"
plainList[5"dodgerblue"
hardList[5"#1E90FF"
// change color language set
function setLang(which) {
    var listObj = document.forms[0].colors
    // filter out old browsers
    if (listObj.type) {
        // find out if it's 3 or 6 entries
        var listLength = listObj.length
        // save selected index
        var currSelected = listObj.selectedIndex
        // replace individual existing entries
        for (var i = 0; i < listLength; i++) {
            if (which == "plain") {
                listObj.options[i].text = plainList[i]
            else {
                listObj.options[i].text = hardList[i]
            }
        }
        if (isPreNN6) {
            history.go(0)
        else {
            listObj.selectedIndex = currSelected
        }
    }
}
// create entirely new options list
function setCount(choice) {
    var listObj = document.forms[0].colors
    // filter out old browsers
    if (listObj.type) {
        // get language setting
        var lang = (document.forms[0].geekLevel[0].checked"plain" "hard"
        // empty options from list
        listObj.length = 0
        // create new option object for each entry
        for (var i = 0; i < choice.value; i++) {
            if (lang == "plain") {
                listObj.options[inew Option(plainList[i])
            else {
                listObj.options[inew Option(hardList[i])
            }
        }
        listObj.options[0].selected = true
        if (isPreNN6) {
            history.go(0)
        }
    }
}
</SCRIPT>
</HEAD>
<BODY>
<H1>Flying Select Options</H1>
<FORM>
Choose a palette size:
<INPUT TYPE="radio" NAME="paletteSize" VALUE=
onClick="setCount(this)" CHECKED>Three
<INPUT TYPE="radio" NAME="paletteSize" VALUE=
onClick="setCount(this)">Six
<P>
Choose geek level:
<INPUT TYPE="radio" NAME="geekLevel" VALUE="" 
onClick="setLang('plain')" CHECKED>Plain-language
<INPUT TYPE="radio" NAME="geekLevel" VALUE="" 
onClick="setLang('hard')">Gimme hex-triplets!
<P>
Select a color:
<SELECT NAME="colors">
    <OPTION SELECTED>cyan
    <OPTION>magenta
    <OPTION>yellow
</SELECT>
</FORM>
</BODY>
</HTML>
Related examples in the same category
1.  Option selected index
2.  Auto Linked Option Listbox without button
3.  Change background
4.  Changing Select Element Content (two Combobox)
5.  Drop-down Redirect - No Submit button
6.  Drop-down Redirect - Submit
7.  Dropdown list (combobox) in a form
8.  dropdown list (combobox) in form 2
9.  Disable or enable an option
10.  Disable and enable a dropdown list (combobox)
11.  Return the name of the form that contains dropdown list (combobox)
12.  Number of options in the dropdown list(Combobox)
13.  Change the size of a dropdown list (ComboBox)
14.  Select multiple options in a dropdown list (option)
15.  Return the selected option as text in option
16.  Return the selected option as a number
17.  Change the option text
18.  Remove an option from a dropdown list (combobox)
19.  Methods and Properties of the Select Object
20.  Properties of the Option Object
21.   Using the Location object to change another frame's URL
22.  Using a Summary Form to Support Local Processing
23.  Navigating with a SELECT Object
24.  Using the selectedIndex Property
25.   Modifying OPTGROUP Element Labels
26.  Using the options[index].text Property
27.   Using the options[index].value Property
28.  Triggering a Color Change from a Pop-Up Menu
29.  Modifying SELECT Options (IE4+)
30.  Modifying SELECT Options (NN6+)
31.  Selecting an Option Using JavaScript
32.  Linked ComboBox (option) Country =- province
33.  ComboBox set value to TextField
34.  Menu(Option) Generator
35.  URL Option ComboBox
36.  Show Selected Option items








Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.