Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

populating a select field 1

Status
Not open for further replies.

WebRic

Technical User
Sep 21, 2004
95
GB
Hi all,

What the best way to completely re-populate a select field?

I've got three select fields and each one will change the others. This is going to happen via ajax so I can use server side code to create any array or what not.

I guess I need to clear the selecct field then re-add the options. I will also need to indicate which option is selected.

Any help is appreciated,
thanks,

R
 
>>What the best way to completely re-populate a select field?

yes, normally it is. But it depends on the use.

If you have 3 dropdowns like Country, State, City it makes sense to clear and add each time as Countries will usuall have different states (names).

And one more thing, why AJAX? why not load all the values in an array on page load? This apporach has both advantages and disadvantages. More can be said only after knowing what exactly you are trying to do...

Known is handfull, Unknown is worldfull
 
Hi vbkris,

There's a huge amount of options that would need to be loaded in the client side which would need to go in the page source as it needs to be created dynamically. I prefer to keep the page source light for search engine reasons, so I thought it best to do this via ajax.

The ajax bit is fine and I can send through the variables to create the results on the server side ajax page. I can return the call fine and process it on the other side. The main bit I'm stuck with is how to clear the select field then add the options back in. I'm guessing I'd need to do this via an array? but am not sure. And I also can't figure out how to clear the current option.

Do you know how to do this?

R

 
to clear a select list:

TheDropDown=document.forms[0].DROPDOWNNAME
while(TheDropDown.length>0)
{
TheDropDown.options[0]=null
}


To Load back data from an Array:

for(i=0;i<TheArray.length-1;i++)
{
TheDropDown.options[TheDropDown.length]=new Option("Text","Value")

}


hope this helps...

Known is handfull, Unknown is worldfull
 
welcome...

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top