473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Adding Option to Select box

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[i],
results[i]);
}
function addOption(selectbox,text,value )
{
var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;
selectbox.options.add(optn);
}
Thanks

Apr 27 '07 #1
1 8351
On Apr 28, 3:08 am, shankwheat <evanbu...@gmail.comwrote:
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[i],
results[i]);
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[i],
results[i]);

}

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));
optn.value = value;
selectbox.options.add(optn);

}

--
Rob

Apr 27 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Cheddar | last post by:
I'm having trouble creating a very simple running total of a few drop down list. I have three seperate dropdown lists, I want the user to simply select some numbers and have the total of the...
3
by: daveland | last post by:
I am working on some JavaScript that dynamically adds rows to a table in response to a button click. A new row does appear on the screen when the button is clicked. However, that table to which a...
6
by: Ben Hallert | last post by:
Hi guys, I'm trying to figure out what bone headed mistake I made on something I put together. I've got a form (named 'context') that has a variable number of select-multiple inputs on it. ...
10
by: David | last post by:
Can anyone give me a quick code snippet (that is standards-based) for adding OPTION tags to a SELECT dynamically. I have no problem doing it in IE but I am kind of new to the whole standards world...
4
by: domas | last post by:
Hi, I can't get my semi-dynamic form to work. Maybe you could help. I've got a simple form with a select box. And a button that, when clicked, creates another copy if the select box and places...
0
by: burgensteen | last post by:
Hi I am after adding a NO THANKS option (that does nothing, just a default select) to the top of my variable option dropdown. My code: <? $query= mysql_query("SELECT DISTINCT username FROM...
1
by: The Eclectic Electric | last post by:
I'd be very grateful if anyone could help me with this. From my limited knowledge of Javascript I don't think it is possible, but I'll punt anyway. I downloaded and very slightly adapted this...
4
tolkienarda
by: tolkienarda | last post by:
hi all I am working on a php driven database program for a literacy program, it will allow them to keep track of classes and students, the part i am strugling with is adding new classes, the...
6
nomad
by: nomad | last post by:
Hello Everyone: I'm having troubles with some coding and Idon't know where it is. I have delevopled a calender where the client can add events to a day. The code worked perfect on my computer but...
3
by: didi86 | last post by:
Please help me to adding multiple row at a time... // Last updated 2006-02-21 <script language="javascript"> function addRowToTable() { var tbl = document.getElementById('tblSample'); ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.