598 Part III . Document (Top ten web hosting) Objects Reference Listing
598 Part III . Document Objects Reference Listing 26-2 (continued) var lang = (document.forms[0].geekLevel[0].checked) ? plain : hard // empty options from list while (listObj.options.length) { listObj.options.remove(0) } // create new option object for each entry for (var i = 0; i < choice.value; i++) { newOpt = document.createElement( OPTION ) newOpt.text = (lang == plain ) ? plainList[i] : hardList[i] listObj.options.add(newOpt) } listObj.options[0].selected = true } } Modifying SELECT options (W3C DOM) Yet another approach is possible in browsers that closely adhere to the W3C DOM Level 2 standard. In NN6, for example, you can use the add()and remove() methods of the SELECT element object. They work very much like the same-named methods for the optionsarray in IE4+, but these are methods of the SELECT element object itself. The other main difference between the two syntaxes is that the NN6 add()method does not use the index value as the second parameter but rather a reference to the OPTION element object before which the new option is inserted. The second parameter is required, so to simply append the new item at the end of the current list, supply nullas the parameter. Listing 26-3 shows the W3C-compatible version of the SELECT element modification scripts shown in Listings 26-1 and 26-2. I highlight source code lines in bold that exhibit differences between the IE4+ and W3C DOM versions. Listing 26-3: Modifying SELECT Options (NN6+) // change color language set function setLang(which) { var listObj = document.forms[0].colors var newOpt // filter out old IE 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++) { newOpt = document.createElement( OPTION ) newOpt.text = (which == plain ) ? plainList[i] : hardList[i] SELECT
In case you need quality webspace to host and run your web applications, try our personal web hosting services.