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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JS populating a select array

Status
Not open for further replies.

Bastien

Programmer
Joined
May 29, 2000
Messages
1,683
Location
CA
Hi All,

Got me a stumper here. I have a form with a series of select boxes on them like

Code:
<SELECT NAME='COMPONENT_TYPE[]' ID='COMPONENT_TYPE[]'  alt='Please enter a component type. If it's not in the list, add it by click the image next to the box'>
<option value='' SELECTED></option>...

What I want to do is to allow users to add to the select option list by click an image, opening a little table and entering the new option. I have all that working, the form shows, captures the data to a js variable etc.

Where I am stumped is accessing the individual select boxes to be able to add the option via this code

Code:
//add the actual option from the above function to the list
function addOption(theSel, theValue)
{
  var newOpt = new Option(theValue, theValue);
  var selLength = theSel.length;
  theSel.options[selLength] = newOpt;
}


I am playing with this code to access the elements but am geting stuck since it doesn't give any details about the object:
Code:
function myfunction(value) 
{ 
  if(value.length<=0)
  {
    return false;
  }else{
    var x = sElement;   //element passed by a 'this' in the inital function call
    alert(sElement);  //trying to get some details about the object
    for (var i=0;i<x.length;i++)
    {
       addOption(sElement[x],value);
    }
  }
}


Bastien

I wish my computer would do what I want it to do,
instead of what I tell it to do...
 
So are you saying that when the image is clicked you'd like to all the new <option> to [!]ALL[/!] the <select> boxes on the page? If not, I'm not sure I understand your question. How are you determining where the new options go? Are the images associated with particular <select> boxes? If so, how? Do you have a link to a page where your code is?

-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
 
I don't understand your question.
Could you please post more code, like the call to myFunction?

<.

 
<input type='button' class='prompt' value='OK' onMouseOver='this.style.border=\"1 outset #dddddd\"' onMouseOut='this.style.border=\"1 solid transparent\"' onClick='" + sendto + "(document.getElementById(\"promptbox\").value);'>


where sendto is the function call that gets passed in the by a prompt call...

So far I have worked out that I can add options to the select that has the image beside it....via:

Code:
    var x =document.getElementById("component_type[]");
    addOption(x,value);

Bastien

I wish my computer would do what I want it to do,
instead of what I tell it to do...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top