Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I change the contents of a listbox? 1

Status
Not open for further replies.

AlexNeil

Programmer
Jun 4, 2001
49
GB
I'd like to change the items that appear in a listbox based on the selection of radio buttons. So, suppose I have a radio button labelled "Alphabet" and another radio button labelled "Numbers", then I'd like the list box to display either the alphabet or numbers depending on what radio button was pressed.

I'd like this to be done using javascript so as to avoid re-loading the page each time.

I guess I need a function (or two?) that is triggered from each of the radio buttons "on change" event. What properties/methods should I use of the listbox to get it to clear it's contents and then load new ones?

Any help much appreciated.
Alex

 
Here is some code for you to fool around with. Try it out to see how you can change the content of a list box.


<html>
<head>
<title>test</title>
<Script Language=&quot;JavaScript&quot;>
<!--
function trim(string)
{
fixedTrim = &quot;&quot;;
lastCh = &quot; &quot;;

for (x = 0; x < string.length; x++)
{
ch = string.charAt(x);
if ((ch != &quot; &quot;) || (lastCh != &quot; &quot;))
{
fixedTrim += ch;
}
lastCh = ch;
}

//alert('Before:' + fixedTrim - 1);
if (fixedTrim.charAt(fixedTrim.length - 1) == &quot; &quot;)
{
fixedTrim = fixedTrim.substring(0, fixedTrim.length - 1);
// alert('After:' + fixedTrim);
}

if(fixedTrim == '')
{
//alert('Please enter a color.');
}
else
{
nameditem = fixedTrim;
insert(nameditem);
//return fixedTrim;
}
}

function insert(nameditem)
{
if (document.frmTest.txt1.value != '' || ' ')
{
var newOption = document.createElement(&quot;OPTION&quot;);
newOption.text = nameditem;
newOption.value = nameditem;
document.frmTest.select1.add(newOption);
document.frmTest.txt1.value = &quot;&quot;;
document.frmTest.txt1.focus();
}
else
{
alert('Please insert a value.');
}
}
function colors(selCol)
{
document.bgColor = selCol;
}
//-->
</Script>
</head>
<body>
<Form name=&quot;frmTest&quot;>
<input type=text value='' name=&quot;txt1&quot;>
<input type=button value=insert onclick=&quot;JavaScript:trim(txt1.value);document.frmTest.txt1.focus();&quot;>
<br>
<select name=&quot;select1&quot; onChange=&quot;colors(this.value);document.frmTest.txt1.focus();&quot; STYLE=&quot;WIDTH: 205px;&quot;>
<Option><------Choose Here-------></Option>
</select>
</Form>
</body>
</html>
 
Cheers Mithrilhall - that works nicely. Do you know what the command is to clear a list box completely? Is it something like:

Code:
document.frmTest.select1.
clear
Code:
;

I've tried guessing the method's name, and had a scan around the web, but I can't find anything.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top