clear an html SELECT and next insert in it all the elements of an array

S

SAN CAZIANO

how can clear an html SELECT and next insert in it all the elements of an array ()
I try this but seems doesn't works.

function ComboAddArrayValueWithLabel(combo,ArrayLabel,ArrayValue)
{
combo.options.length = 0;
for (i=0; i<ArrayLabel.length; i++)
combo.options[combo.options.length] = new Option(ArrayLabel,ArrayValue);
}
 
G

Grant Wagner

SAN said:
how can clear an html SELECT and next insert in it all the
elements of an array ()I try this but seems doesn't works.
function
ComboAddArrayValueWithLabel(combo,ArrayLabel,ArrayValue){combo.options.length
= 0;for (i=0; i<ArrayLabel.length; i++)
combo.options[combo.options.length] = new
Option(ArrayLabel,ArrayValue);}


It's usually best to provide a complete example that demonstrates
the problem you are experiencing, and a complete explanation of
the behavior you are experiencing, and how it differs from the
behavior you are expecting (<url:
http://www.physci.org/codes/sscce.jsp />). For example:

<form name="myForm">
<select name="mySelect">
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
</select>
<input type="button" name="myTest"
value="Test"
onclick="test(this.form.elements['mySelect'], labels,
values);">
</form>
<script type="text/javascript">
var labels = [
'four', 'five', 'six'
];
var values = [
4, 5, 6
];

function test(combo, ArrayLabel, ArrayValue) {
combo.options.length = 0;
for (var i = 0; i < ArrayLabel.length; ++i) {
combo.options[combo.options.length] =
new Option(ArrayLabel, ArrayValue);
}
// combo.options[0].selected = true;
}
</script>

Works as shown in IE 6.0.2800, Firefox 1.0, Opera 7.54 and
Netscape 4.78 (if you uncomment the last line of -test-).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top