Jump to content

Append drop down options string via javascript


Master81

Recommended Posts

Hi

 

Right I'm using ajax and php to dynamically populate my drop down list. All is working except one thing. I am trying to send the <option> tags (in an array) by echoing this out to the javascript file.

 

Now I know in javascript I can create new options by using the createElement option but how do I tell javascript that I want to basically append the <option> tags into the droplist?

 

If anyone has any thoughts on how I can do this, that would be great. Thanks.

Link to comment
Share on other sites

I fought that for the longest time and then i found the "new Option()" way

so, I just bring back the option values in the xml and I clear out the current, and refill it.

you can do it other ways, like just doin the new Option()

 

here is code I use all the time (cross browsers)

var lSelectObj = document.getElementById('dropdown');
lSelectObj.options.length = 0
for (j=0;j<x.length;j++)
{
   vNewValues = optionVal[j];
	   vNewText = optionText[j]
           lSelectObj.options[j] = new Option(vNewText,vNewValues);
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.