Click to See Complete Forum and Search --> : URGENT?? PLS HELP! How to get multiple values from listbox?


demang
08-20-2003, 10:40 PM
Hello friend.. I have a problem here. How can I get multiple values from selected item from a list box? I've tried some ways but it didn't work.. anyway I never write code using javascript.. pls help..

demang

Code example I did but fail..

<html>
<head>
<script language="javascript">

function getSelectedValues (select)
{
var r = new Array();
for (var i = 0; i < select.options.length; i++)
if (select.options[i].selected)
{
r[r.length] = select.options[i].value;
}
return r;
}

</script>

<script language="javascript">

function getSelectedValues2(list){
var len = list.length;
var ctr=0;
var values = new Array();
for(x=0;x<len;x++){
if(list[x].selected && list[x].value!="-1"){
values[ctr] = list[x].value;
ctr++;
}
}
return values;
}

function testSelect2 (form) {

alert( form.customer_type.options.length );

for (x=0;x<form.customer_type.options.length;x++){
alert(form.customer_type.options[customer_type.selectedIndex.value]);
if (form.customer_type.selectedIndex.selected) {
Item = form.customer_type.selectedIndex[x];
Result = form.customer_type.options[Item].text;
alert (Result);
}
}

}
</script>

</head>

<body>
<form name="myForm">
<select name="customer_type" size="5" multiple="true">
<option selected>Customer Type
<option value="Goodly">Goodly
<option value="Badly">Badly
<option value="Uglyly">Uglyly
</select>

<!--<input type="button" name"button1" value="Get Value!" OnClick="myArray=getSelectedValues(this.form)">-->
<input type="button" name"button2" value="Get Value!" onClick="testSelect2(this.form)">

</form>

Exuro
08-20-2003, 11:57 PM
The getSelectedValues function works, but you probably just didn't know what to pass in. Here's what your button would look like:

<input type="button" name"button2" value="Get Value!" onClick="alert(getSelectedValues(this.form.customer_type))">

demang
08-21-2003, 01:36 AM
thank you Exuro... if I can add some more question, I'm trying to put those selected values in a different line only with one click... how can I do that?

Exuro
08-21-2003, 01:51 AM
Originally posted by demang
I'm trying to put those selected values in a different line only with one click... how can I do that?

What exactly do you mean by "putting them in a different line"??? I guess would would really probably help is knowing what you're trying to do with the data. Are you trying to put it in a textbox? Or maybe print it out on the screen? Or are you just trying to get it to pop up in an alert box like we had it?

demang
08-21-2003, 02:04 AM
Actually I want it to be printed on screen in different line. Means, if I select 3 items, Goodly, Badly and Uglyly, it will print

Goodly

Badly

Uglyly


Currently with alert box it display "Goodly,Badly,Uglyly".

Should I split the result or how?

Exuro
08-21-2003, 05:05 PM
The alert box is displaying the entire array, which is why they are separated by commas like that. If you want to be able to break them up what you should do is get each value in the array individually and then display them how you'd like them. Here's an example:

<html>
<head>
<script type="text/javascript">
<!--
function getSelectedValues (select)
{
var r = new Array();
for (var i = 0; i < select.options.length; i++)
if (select.options[i].selected)
{
r[r.length] = select.options[i].value;
}
return r;
}

function display(listData) {
var str="";
for (i=0;i<listData.length;i++) {
str+=listData[i]+"<br>\n";
}
document.getElementById('textDiv').innerHTML=str;
}
//-->
</script>
</head>
<body>
<form name="myForm">
<select name="customer_type" size="5" multiple="true">
<option selected>Customer Type
<option value="Goodly">Goodly
<option value="Badly">Badly
<option value="Uglyly">Uglyly
</select>
<input type="button" name"button2" value="Get Value!" onClick="display(getSelectedValues(this.form.customer_type))">
</form>
<div id="textDiv"></div>
</body>
</html>

demang
08-21-2003, 08:37 PM
thanks a lot Exuro. You make me happy... :D