Adding Option to Select box

S

shankwheat

I'm trying to add an option to a select box dynamically. A typical
string value is delimitted by a # character. There will always be
only one option to add. What's happening is that the option text value
shows up twice in the selectbox.


I'm getting this:

<select id="ddlSavedBenchmarks">
<option>mytext</option>
<option>28</option>
</select>

Instead of this:


<select id="ddlSavedBenchmarks">
<option value="28">mytext</option>
</select>


results = "mytext#28"

// Split the delimited response into an array
results = xmlHttp.responseText.split("#");

for (var i=0; i < results.length;++i){
addOption(document.choiceForm.ddlSavedBenchmarks, results,
results);
}


function addOption(selectbox,text,value )
{
var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;
selectbox.options.add(optn);
}


Thanks
 
R

RobG

I'm trying to add an option to a select box dynamically. A typical
string value is delimitted by a # character. There will always be
only one option to add. What's happening is that the option text value
shows up twice in the selectbox.

I'm getting this:

<select id="ddlSavedBenchmarks">
<option>mytext</option>
<option>28</option>
</select>

Instead of this:

<select id="ddlSavedBenchmarks">
<option value="28">mytext</option>
</select>

results = "mytext#28"

// Split the delimited response into an array
results = xmlHttp.responseText.split("#");

for (var i=0; i < results.length;++i){
addOption(document.choiceForm.ddlSavedBenchmarks, results,
results);


Your example has different values for the option's text and value
attributes, but this code should make them the same - which do you
want? Anyhow, use something like:

var sel = document.choiceForm.ddlSavedBenchmarks;
sel.options[sel.options.length] = new Option(results,
results);

}

function addOption(selectbox,text,value )
{
var optn = document.createElement("OPTION");
optn.text = text;

Using this method, IE will not properly add the text property, use:

optn.appendChild(document.createTextNode(text));
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top