Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Changing text in selectbox

Reply
Thread Tools

Changing text in selectbox

 
 
shankwheat
Guest
Posts: n/a
 
      11-09-2006
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);
}
}

 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      11-09-2006
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

 
Reply With Quote
 
 
 
 
shankwheat
Guest
Posts: n/a
 
      11-09-2006
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


 
Reply With Quote
 
shankwheat
Guest
Posts: n/a
 
      11-09-2006
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


 
Reply With Quote
 
mick white
Guest
Posts: n/a
 
      11-09-2006
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]
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copying Selectbox options to another selectbox shankwheat Javascript 2 11-29-2006 05:16 PM
Changing Value of a Selectbox shankwheat Javascript 1 06-22-2006 07:06 PM
Re: Javascript dropdown getting cut off by selectbox Bodidily HTML 0 08-25-2004 11:02 AM
Javascript dropdown getting cut off by selectbox danparker276@yahoo.com HTML 2 08-24-2004 10:42 PM
how to fix it:SelectBox always on top of dropdown menu item. Matthew ASP .Net 3 08-06-2003 05:23 AM



Advertisments