Monday, November 7, 2011

List Boxes Selection 2

<html>
<head>
<title>List Boxes Selection </title>
</head>
<body>

<script>

var ListUtil = new Object();

function listBoxMani()
{

alert(" listBoxMani() ");

var oListbox = document.getElementById("selAge");

alert(" oListbox "+ ListUtil.getSelectedIndexes(oListbox));

}

ListUtil.getSelectedIndexes = function (oListbox)
{
var arrIndexes = new Array;
for (var i=0; i < oListbox.options.length; i++)
{
if(oListbox.options[i].selected)
{
arrIndexes.push(i);
}
}
return arrIndexes;
};

</script>

<form name="ListBoxes_form1">


<select name="selAge" id="selAge" size="2" multiple="multiple">
<option value="18-21">18-21</option>
<option value="22-25">22-25</option>
<option value="26-29">26-29</option>
<option value="30-35">30-35</option>
<option value="Over 35">Over 35</option>

</select>

<input type="button" value="getSelectedIndex" onClick="listBoxMani();">


</form>

</body>
</html>


Retrieving the selected option(s)



selectedIndex is an attribute of <select> element which contains the index of selected Index, as we previously saw oListbox.selectedIndex


If selection box type is list box ( list box by setting the multiple attribute of the <select/> element to "multiple") ( not for combo box )means it is possible to select more than one option

if multiple options are selected , by using oListbox.selectedIndex we can get only the first selected item , but that really doesn̢۪t help.

What we need is a way to get the index of all the selected options ,to get the index writing an custom method will solve all the problem

For this, you need a custom method, which is the first for the ListUtil object. The getSelectedIndexes() method takes advantage of another special property of the

No comments: