473,396 Members | 2,030 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,396 software developers and data experts.

Changing text in selectbox

I'm passing a string like 'Div3,Div4' to use as options in a selectbox
but I need the text the user sees in the box different from the values.
My code populates the values in the selectbox correctly but not the
text. Thanks

Desired end result:

<select id="selectBox" multiple="multiple" size="4">
<option value="Div3">About the Company</option>
<option value="Div1">Ratings</option>
</select>

I tried this but it's not working. No errors

optionsString = "Div3,Div1";
optionsArray = optionsString.split(",");
var sb = document.getElementById("selectBox");
for (i=0; i < optionsArray.length; i++) {
var opt = document.createElement("OPTION");
opt.setAttribute("value",optionsArray[i]);

var commonName = optionsArray[i];
if (commonName == "Div1") {
opt.setAttribute("text","Ratings");
} else if (commonName == "Div3") {
opt.setAttribute("text","About the Company");
}
opt.appendChild(document.createTextNode(optionsArr ay [i]));
sb.appendChild(opt);
}
}

Nov 9 '06 #1
4 1718
shankwheat wrote:
I'm passing a string like 'Div3,Div4' to use as options in a selectbox
but I need the text the user sees in the box different from the values.
My code populates the values in the selectbox correctly but not the
text. Thanks
IE is buggy in this regard. Use either:

select.option[i]=new Option(text[,value[,defaultSelected[,selected]]])

where defaultSelected and selected are booleans. This method is widely
used because not only is it convenient, but it works everywhere (AFAIK)
and you only need to include the parameters you want - typically just
text and value.

If you want to stick with createElement, add the option text by
appending a text node set to the required value, e.g.

var opt = document.createElement('option');
opt.value = 'some value';
opt.appendChild(document.createTextNode('some text'));

--
Rob

Nov 9 '06 #2
Thanks. I've chamged things around to use your suggested the
select.option[i]=new Option(text[,value[,defaultSelected[,selected]]])
method but the text value for the options isn't appear in the
selectbox, just the values. Thanks

optionsString = "Div3,Div1";
optionsArray = optionsString.split(",");
var sb = document.getElementById("selectBox");
for (i=0; i < optionsArray.length; i++) {

var commonName = optionsArray[i];
if (commonName = "Div1") {
var text = "Ratings";
} else if (commonName = "Div3") {
var text = "About the Company";
}

sb.options[i] = new Option(optionsArray[i],text);
}
}

RobG wrote:
shankwheat wrote:
I'm passing a string like 'Div3,Div4' to use as options in a selectbox
but I need the text the user sees in the box different from the values.
My code populates the values in the selectbox correctly but not the
text. Thanks

IE is buggy in this regard. Use either:

select.option[i]=new Option(text[,value[,defaultSelected[,selected]]])

where defaultSelected and selected are booleans. This method is widely
used because not only is it convenient, but it works everywhere (AFAIK)
and you only need to include the parameters you want - typically just
text and value.

If you want to stick with createElement, add the option text by
appending a text node set to the required value, e.g.

var opt = document.createElement('option');
opt.value = 'some value';
opt.appendChild(document.createTextNode('some text'));

--
Rob
Nov 9 '06 #3
I've changed this around again because I found this method the easiest
to follow. However, the text value for both options in the selectbox
is "Ratings" and the first one should be "About the Company" and the
second one "Ratings". I think I'm just messing up the DivValue
assignment.

optionsString = "Div3,Div1";
optionsArray = optionsString.split(",");
for (i=0; i < optionsArray.length; i++)
{

NewOpt = new Option;
NewOpt.value = optionsArray[i];
var DivValue = optionsArray[i];
alert(DivValue);
if (DivValue = "Div1")
{
var optionText = "Ratings";
}
else if (DivValue = "Div3")
{
var optionText = "About the Company";
}

NewOpt.text = optionText;
document.form1.selectBox.options[i] = NewOpt;

shankwheat wrote:
Thanks. I've chamged things around to use your suggested the
select.option[i]=new Option(text[,value[,defaultSelected[,selected]]])
method but the text value for the options isn't appear in the
selectbox, just the values. Thanks

optionsString = "Div3,Div1";
optionsArray = optionsString.split(",");
var sb = document.getElementById("selectBox");
for (i=0; i < optionsArray.length; i++) {

var commonName = optionsArray[i];
if (commonName = "Div1") {
var text = "Ratings";
} else if (commonName = "Div3") {
var text = "About the Company";
}

sb.options[i] = new Option(optionsArray[i],text);
}
}

RobG wrote:
shankwheat wrote:
I'm passing a string like 'Div3,Div4' to use as options in a selectbox
but I need the text the user sees in the box different from the values.
My code populates the values in the selectbox correctly but not the
text. Thanks
IE is buggy in this regard. Use either:

select.option[i]=new Option(text[,value[,defaultSelected[,selected]]])

where defaultSelected and selected are booleans. This method is widely
used because not only is it convenient, but it works everywhere (AFAIK)
and you only need to include the parameters you want - typically just
text and value.

If you want to stick with createElement, add the option text by
appending a text node set to the required value, e.g.

var opt = document.createElement('option');
opt.value = 'some value';
opt.appendChild(document.createTextNode('some text'));

--
Rob
Nov 9 '06 #4
shankwheat wrote:
I've changed this around again because I found this method the easiest
to follow. However, the text value for both options in the selectbox
is "Ratings" and the first one should be "About the Company" and the
second one "Ratings". I think I'm just messing up the DivValue
assignment.
See below:
>
optionsString = ;
optionsArray = optionsString.split(",");
//optionsArray = ["Div3,Div1"]
for (i=0; i < optionsArray.length; i++)
//You seem to be declaring a global variable of "i", this may be a problem
{

NewOpt = new Option;
NewOpt.value = optionsArray[i];
var DivValue = optionsArray[i];
alert(DivValue);
if (DivValue = "Div1")
//if (DivValue == "Div1")
// or better yet:
//if ( "Div1"==optionsArray[i])
//
Mick
{
var optionText = "Ratings";
}
else if (DivValue = "Div3")
//if (DivValue == "Div3")
{
var optionText = "About the Company";
}


>
NewOpt.text = optionText;
document.form1.selectBox.options[i] = NewOpt;
[snip]
Nov 9 '06 #5

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

Similar topics

2
by: Tole | last post by:
hi all, i've got a selcetbox (multiple) which is filled by javascript. only problem is that i have aprox 1000 options to add to that select, and that adding lasts for 5-6 seconds. Even...
7
by: Erwin Moller | last post by:
Hi, situation: I have a long list of options in multiple selectbox. The selectbox gets a scrollbar in that situation. Does anybody know if it is possible to scroll through the options with...
3
by: Stefan Richter | last post by:
I would like to fill a Selectbox dynamically. I am taking all values from a db, and fill them into a selectbox. I guess the best way was to use some kind of a dynamical array, where you don't have...
4
by: Robert Bravery | last post by:
HI all, In JS, how can I change the selected value of a dropdownlist(select options) I know the value of indexed value of the selected value before it is changed. Thanks Robert
1
by: shankwheat | last post by:
I've been using this little script to change the <a href=""></a> value of a link which works well. <script language="javascript" type="text/javascript"> function ordering(sorder) { var link =...
1
by: mamidira | last post by:
I had 3 select boxes in my form where all the 3 are dependent on the above select box. i.e., the values in the second select box is dependent on the selection of the first selectbox and same with...
1
by: giloosh | last post by:
i would like to dynamically output such a selectbox below using php and mysql: <select> <option< 100 </option> <option100 to 150 </option> <option150 to 200 </option> <option200 to 250...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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.