views:

50

answers:

1

I want to make the options of < select > show or hide by program(JS),is it possible ?

it just like the interesting tags in the front page of StackOver Flow,when you type in some words,the drop list will expand and give you suggestions.

ps.I know StackOver didn't use select in this case,anyway,just take it as an example.

+1  A: 

You add or remove items from the options collection of the select.

Here is an example that removes one item and adds another:

<html>
<head>
<title></title>
<script type="text/javascript">

function init() {
  // get a reference to the element
  var sel = document.getElementById('sel');
  // remove an option
  sel.options[2] = null;
  // create a new option and add to the select
  var opt = document.createElement('option');
  opt.value = '5';
  opt.text = 'five';
  sel.options.add(opt);
}

</script>
</head>
<body onload="init();">
<form>

<select id="sel">
  <option value="0">zero</option>
  <option value="1">one</option>
  <option value="2">two</option>
  <option value="3">three</option>
  <option value="4">four</option>
</select>

</form>
</body>
</html>
Guffa
Seems that you didn't get my point,anyway, thanks for your reply.
DiveInto
@DiveInto: In that case I still don't. What point are you talking about, and what is it?
Guffa