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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SELECT BOX MULTIPLE

Status
Not open for further replies.

Greenster

Programmer
Jan 14, 2002
30
0
0
GB
In Javascript I am looping thru a SELECT MULTIPLE form, I either want to find out the total number of options in this select box or have a check hwich says:

IF ISSET (document.mainform.did_multiple.value){
do some stuff
}

Any help would be appreciated

Thanx
{Greenster}
 
This sample (cut and paste into new html page) will tell you how many options there are, how many are selected and perform a logical check on the selections.
Hope it will help

<BODY>
<form name=&quot;F1&quot; >
<select size=&quot;5&quot; name=&quot;D2&quot; onChange=&quot;testme2();&quot; multiple>
<option>Line1</option>
<option>Line2</option>
<option>Line3</option>
<option>Line4</option>
<option>Line5</option>
<option>Line6</option>
<option>Line7</option>
<option>Line8</option>
<option>Line9</option>



</select>
</Form>


<Script language=&quot;JavaScript&quot;>
function testme2()
{
var MyVar;
MyVar = 0;
alert(&quot;Total Options = &quot; + document.F1.D2.options.length);
for (var i = 0; i < document.F1.D2.options.length; i++)
{
if (document.F1.D2.selected)
{
MyVar = MyVar + 1;
if (document.F1.D2.text == &quot;Line3&quot;)
{
alert(&quot;Line&quot; + (i+1) + &quot; selected - Do some stuff&quot;);
}
}
}
alert(&quot;Total Options Selected = &quot; + MyVar);
}
</Script>
</Body>
 
Here it is:

<script>
function getSelected(obj) {
var count = 0;
var returnList = '';

for (var current=0; current < obj.options.length; current++)
{
if (obj.options[current].selected)
{
count++;
returnList += '[ ' + current + ' ] ';
}
}

alert(count + ' selected items from ' + obj.options.length + '\n' + returnList);
}
</script>


<body>
<form>
. . .
<select name=&quot;mySelect&quot; multiple>
. . .
</select>

<input type=button value=&quot;get selected&quot; onclick=getSelected(this.form.mySelect)>
</form>
</body>


Alert box will show total number of selected options and their order numbers in options array.

good luck
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top