Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Get Values from Multiple Select Option Box

Tags:

This one is driving me nuts. It’s got to be something simple and stupid that I am overlooking. I have a multiple select box in a form. I am just trying to get the values that are selected. In my loop, if I use alert then I have no problem. As soon as try to concatenate the values I get the error ‘SelBranch[...].selected' is null or not an object

      <form name="InventoryList" method="post" action="InventoryList.asp">
          <select name="SelBranch" class="bnotes" size="5" multiple="multiple">
          <option value="All">All</option>
          <option value="001 Renton">001 Renton</option>
          <option value="002 Spokane">002 Spokane</option>
          <option value="003 Missoula">003 Missoula</option>
          <option value="004 Chehalis">004 Chehalis</option>
          <option value="005 Portland">005 Portland</option>
          <option value="006 Anchorage">006 Anchorage</option>
          <option value="018 PDC">018 PDC</option>
          </select>

         <input type="button" name="ViewReport" value="View" class="bnotes" onclick="GetInventory();">

   </form>


   <script language="JavaScript">
       function GetInventory()
       {
         var InvForm = document.forms.InventoryList;
         var SelBranchVal = "";
         var x = 0;

         for (x=0;x<=InvForm.SelBranch.length;x++)
         {
            if (InvForm.SelBranch[x].selected)
            {
             //alert(InvForm.SelBranch[x].value);
             SelBranchVal = SelBranchVal + "," + InvForm.SelBranch[x].value;
            }
         }
         alert(SelBranchVal);
       }


  </script>
like image 278
Soren Avatar asked Mar 16 '11 18:03

Soren


People also ask

How can I get multiple selected values of select box in jQuery?

With jQuery, you can use the . val() method to get an array of the selected values on a multi-select dropdown list.

How can we select multiple values from dropdown?

To select multiple options in a drop-down list, use the multiple properties. It allows you to select more than one option while pressing CTRL key.

What is the return type of get all selected options?

selectedOptions returns a list of selected items. Specifically, it returns a read-only HTMLCollection containing HTMLOptionElements. ... is spread syntax. It expands the HTMLCollection 's elements.


2 Answers

The for loop is getting one extra run. Change

for (x=0;x<=InvForm.SelBranch.length;x++)

to

for (x=0; x < InvForm.SelBranch.length; x++)
like image 79
amit_g Avatar answered Sep 22 '22 18:09

amit_g


Here i am posting the answer just for reference which may become useful.

<!DOCTYPE html>
<html>
<head>
<script>
function show()
{
     var InvForm = document.forms.form;
     var SelBranchVal = "";
     var x = 0;
     for (x=0;x<InvForm.kb.length;x++)
         {
            if(InvForm.kb[x].selected)
            {
             //alert(InvForm.kb[x].value);
             SelBranchVal = InvForm.kb[x].value + "," + SelBranchVal ;
            }
         }
         alert(SelBranchVal);
}
</script>
</head>
<body>
<form name="form">
<select name="kb" id="kb" onclick="show();" multiple>
<option value="India">India</option>
<option selected="selected" value="US">US</option>
<option value="UK">UK</option>
<option value="Japan">Japan</option>
</select>
<!--input type="submit" name="cmdShow" value="Customize Fields"
 onclick="show();" id="cmdShow" /-->
</form>
</body>
</html>
like image 29
Kaushal Bhatt Avatar answered Sep 22 '22 18:09

Kaushal Bhatt