Selecting items in a listbox

M

Mark

Hi - is it possible to pass a comma delimeted list to a javascript
function, which will then loop through the list, and check against a
listboxs items, and if the value in the list, corresponds to an item in
the listbox, the listbox item should be 'selected' - I'd like this to
happen for a multiple select list box.

Something along the lines of (sorry, I'm not knowledgable in JS just
yet, but need to solve the problem):

function selItems()
{
selItemsInList('1,4,4,7,8');
}

selItemsInList(str)
{
for each checkitem in str
{
for each item in selectlist
{
if item.value == checkitem.value
{
selectlist.item.checked;
}
}
}

Thanks for any help,
 
M

Mark

Hi - I've managed to get this working to an extent - I'm just not sure
how to get a comma delimmetted list, and iterate through that - here's
what I have so far, taking info from hard coded variables:

function pop(inForm)
{
var option0 = "1" //this is the part I want to be read from a comma
list
var option1 = "6"
var option2 = "3"
for (var i=0; i < 3; i++) {
for (var x=0; x < 3; x++)
{
if (inForm.lbMT.options.value==eval("option" + x))
{
alert("equal so selecting");
inForm.lbMT.options.selected=true;
}
}
}
}
 
M

Matt Kruse

Mark said:
Hi - is it possible to pass a comma delimeted list to a javascript
function, which will then loop through the list, and check against a
listboxs items, and if the value in the list, corresponds to an item in
the listbox, the listbox item should be 'selected'

You can use my general setInputValue() function on multiple select boxes,
like this:
setInputValue(document.forms[0].mySelect,"a,d,h");

The function, along with others, can be found in my validations lib at:
http://www.mattkruse.com/javascript/validations/
 
M

Mark

Hi Matt - thank you for this.

I tried the setInputValue on your site, and entered 1,3 and "1,3" - but
only one of the select items hilited - have I entered the numbers
incorrectly?

Thanks again for the help,
 

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,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top