Link to home
Start Free TrialLog in
Avatar of ronpenrose
ronpenrose

asked on

Getting Selected Listbox Items in Javascript

I am cannot figure out how to get multiple selected items in a listbox on ASP page.  I am using document.Form1.lstMyList.Options(i).value but each item value is empty.

example code

for (i=0;i<document.Form1.lstMyList.Length;i++) {
  if document.Form1.lstMyList.options(i).selected = true) {
      alert(document.Form1.lstMyList.Options(i).value);
  }
Avatar of jrram
jrram
Flag of United States of America image

Should be:

      for (i=0; i < document.Form1.lstMyList.length; i++)
      {
              if (document.Form1.lstMyList.options(i).selected)
              {
                  alert(document.Form1.lstMyList.options(i).value);
              }
      }
And remember that Javascript is case-sensitive, so ".Length" should be ".length" and ".Options(i)" should be ".options".

Also, if you are trying to compare to see if two values are equal, it should be "==" instead of "="
ASKER CERTIFIED SOLUTION
Avatar of WoodyRoundUp
WoodyRoundUp

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial