PDA

Click to See Complete Forum and Search --> : Simple question on javascript


jdavison
Sep 7th, 2000, 04:20 PM
How do I remove all the options in a select. basically I want to remove all the set options and then repopulate it. I can get it to populate.

dadames
Sep 8th, 2000, 02:49 PM
How are you populating it? If it's dynamic then it always starts from an empty list.

jdavison
Sep 8th, 2000, 07:57 PM
I am populating it through a javascript so it can reset the values in the list. I want to erase the list when a new option is selected and then repopulate it. If I don't dumb the list, it keep some of the old options if the list is shorter.

dadames
Sep 11th, 2000, 06:21 AM
Are the values hard-coded? If so you could set up conditions in combining your html and script e.g.

pseudo-code...

<select>
if (condition1)
<OPTION value="bob">Bob
<OPTION value="jimbob">Jim
Else
<OPTION value="jo">Jo
<OPTION value="david">David
</select>

Mark Sreeves
Sep 11th, 2000, 07:37 AM
if you have a look at http://www.parkers.co.uk
select used car prices and then view the code

it does exactly what you want.

It looks a though it is re-populating iover original values and then re-sizing the select box

monte96
Sep 12th, 2000, 10:49 AM
You need to loop through starting at the last option element. That should keep the resizing of it from causing an error.


for i = frm.lstSelect.Options.Length - 1 to 0 Step -1
frm.lstSelect.Options.Remove i
Next

HarryW
Sep 12th, 2000, 11:36 AM
Except in JavaScript of course :)

Mark Sreeves
Sep 13th, 2000, 02:32 AM
<HEAD>
<script Language="javaScript">
var i;
function fillList()
{
i++;
var oOption;
oOption = document.createElement("OPTION");
oOption.text="Apples";
oOption.value=i;
document.frm1.select1.add(oOption);
}

function clearList()
{

document.frm1.select1.length = 1;
i=0;

}
</script>
</head>
<BODY>
<form name="frm1">
<SELECT id=select1 name=select1>
<option>Please select</option>
</SELECT>
</form>
<input type="button" onclick="fillList()" value="Add item">
<input type="button" onclick="clearList()" value="clear list" id=button1 name=button1>
</BODY></HTML>


[Edited by Mark Sreeves on 09-13-2000 at 03:36 AM]