Changing text in selectbox

S

shankwheat

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);

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

RobG

shankwheat said:
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=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'));
 
S

shankwheat

Thanks. I've chamged things around to use your suggested the
select.option=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;
if (commonName = "Div1") {
var text = "Ratings";
} else if (commonName = "Div3") {
var text = "About the Company";
}

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


shankwheat said:
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=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'));
 
S

shankwheat

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;
var DivValue = optionsArray;
alert(DivValue);
if (DivValue = "Div1")
{
var optionText = "Ratings";
}
else if (DivValue = "Div3")
{
var optionText = "About the Company";
}

NewOpt.text = optionText;
document.form1.selectBox.options = NewOpt;


Thanks. I've chamged things around to use your suggested the
select.option=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;
if (commonName = "Div1") {
var text = "Ratings";
} else if (commonName = "Div3") {
var text = "About the Company";
}

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


shankwheat said:
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=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'));
 
M

mick white

shankwheat said:
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;
var DivValue = optionsArray;
alert(DivValue);
if (DivValue = "Div1")


//if (DivValue == "Div1")
// or better yet:
//if ( "Div1"==optionsArray)
//
Mick


{
var optionText = "Ratings";
}
else if (DivValue = "Div3")

//if (DivValue == "Div3")
{
var optionText = "About the Company";
}




NewOpt.text = optionText;
document.form1.selectBox.options = NewOpt;

[snip]
 

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

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top