js function with multiple select

I have this code

<html>
 <head>
  <script type="text/javascript">

   function changeFunc() {
    var selectBox = document.getElementById("selectBox");
    var selectedValue = selectBox.options[selectBox.selectedIndex].value;
    alert(selectedValue);
   }

  </script>
 </head>
 <body>
  <select id="selectBox" multiple onchange="changeFunc();">
   <option value="1">Option #1</option>
   <option value="2">Option #2</option>
   <option value="3">Option #3</option>
   <option value="4">Option #4</option>
  </select>
 </body>
</html>

Open in new window


When I make the first selection the alert comes up ok. But with multiple selection it always alerts with the topmost selection.

How can I file the function with the selection I am making?
robrodpAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GaryCommented:
You can't in IE, all you can get is the first selected option or an array of the selected option values.
0
Ravi KallaTechnology LeadCommented:
<html>
 <head>
  <script type="text/javascript">

   function changeFunc() {
    var select1 = document.getElementById("selectBox");
var tmpStr = "";
for (var i = 0; i < select1.length; i++) {
        if (select1.options[i].selected)
            tmpStr += select1.options[i].value;
    }
    alert(tmpStr);
   }

  </script>
 </head>
 <body>
  <select id="selectBox" multiple onchange="changeFunc();">
   <option value="1">Option #1</option>
   <option value="2">Option #2</option>
   <option value="3">Option #3</option>
   <option value="4">Option #4</option>
  </select>
 </body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GaryCommented:
We are talking about alerting the value of the option they clicked even if other options are already selected?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

robrodpAuthor Commented:
That is right
0
GaryCommented:
So my previous comment stands - it's not possible in IE, works in FF et al but it is not part of the spec that browsers allow this so shouldn't be used in case the browsers drop support for it.
0
robrodpAuthor Commented:
Does the trick

Thx
0
GaryCommented:
I thought you wanted only the value of the option they clicked not all the values together?
0
robrodpAuthor Commented:
Hi Gary

Yes that is what I need. I changed the code eliminating the "+" that concatenates the values and it works. I tried it in ie and it is working... so far.  I think you do not recommend this approach. Am I right?
0
GaryCommented:
That code doesn't do that, it will end up giving you the value of the last selected option value in the list.
Try it - click 2 then 4 then 1 - it will alert 2 then 4 then 4

It cannot be done in IE
0
robrodpAuthor Commented:
Right.

Then is each time it runs the function I want it to run an ajax program with that specific variable. I am getting there. I just posted another question to that regard.

Thx Gary
0
GaryCommented:
That will not work with a dropdown, your option is to use a list e.g.

<ul>
<li>Option1</li>
<li>Option2</li>
<li>Option3</li>
<li>Option4</li>
</ul>

Open in new window

I think this question should be reopened as the accepted answer is incorrect.
0
robrodpAuthor Commented:
How can I close it?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.