469,951 Members | 2,717 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,951 developers. It's quick & easy.

How do I change contents of pulldown?

Hi

I need to clear the contents of a pulldown and then load new contents
into it. Is this possible in Javascript? If so how?

Thanks

Nick Steele
Jul 23 '05 #1
2 1178
ns*******@hotmail.com wrote:
Hi

I need to clear the contents of a pulldown and then load new contents
into it. Is this possible in Javascript? If so how?


Yes, it is, but in what format are the replacement items?
function clearMenu(menu,keepHeader){
menu.length=keepHeader?1:0;
}

function replaceMenu(menu,array,keepHeader){
clearMenu(menu,keepHeader);
a=array.length;
for(i=0;i<a;i++){
menu.options[menu.length]=new Option(array[i],array[i]);
}
}
Consider a select menu named "foo"

<HTMLelement
onclick="replaceMenu(document.forms[0].foo,['select a number',1,2,3,4])">

Mick
Jul 23 '05 #2

<ns*******@hotmail.com> wrote in message
news:i1********************************@4ax.com...
Hi

I need to clear the contents of a pulldown and then load new contents
into it. Is this possible in Javascript? If so how?

Thanks

Nick Steele


Yes it's possible :o)

something like this you'r looking for ?
<script type="text/javascript">
newOpt = []; //array with new values
newOpt[0] = "newVal1"
newOpt[1] = "newVal2"
newOpt[2] = "newVal3"
newOpt[3] = "newVal4"
function changeOpt(){
opt = document.frm.whee.options
for(var i=0;i<newOpt.length;i++){
opt[i]=new Option(newOpt[i],newOpt[i]);
}
for(i=i;i<opt.length;i++)
opt[i]=null
}
</script>

<form name="frm">
<select name="whee">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</form>

<a href="#" onclick="changeOpt();return false">Newvalue</a>

Best Regards

Nick
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Richard MSL | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.