-8

When I try to select two options it gives me the first option two times.

Here is the code :

<head>
  <script type="text/javascript">
    function choices() {
      var y = document.getElementById("s");
      var x = document.getElementById("output");
      var choice = y.value;
      for (i = 1; i <= 3; i++) {
        var current = y[i];
        if (current.selected == true) {
          x.innerHTML += "the language is, " + choice + "<br>";
        }
      }
    }

  </script>
</head>

<body>
  <form action="">
    <fieldset>
      <select id="s" multiple="multiple" size="4">
        <option>--Choose language--</option>
        <option value="HTML">HTML</option>
        <option value="JAVA">JAVA</option>
        <option value="PHP">PHP</option>
      </select>
      <input type="button" value="show" onclick="choices()">
    </fieldset>
  </form>
  <div id="output"></div>

</body>
1

2 Answers 2

4

If you change

x.innerHTML += "the language is, " + choice + "<br>";

to

x.innerHTML += "the language is, " + current.value + "<br>";

it will work.

function choices() {
  var y = document.getElementById("s");
  var x = document.getElementById("output");
  var choice = y.value;
  for (i = 1; i <= 3; i++) {
    var current = y[i];
    if (current.selected == true) {
      x.innerHTML += "the language is, " + current.value + "<br>";

    }
  }
}
<form action="">
  <fieldset>
    <select id="s" multiple="multiple" size="4">
        <option>--Choose language--</option>
        <option value="HTML">HTML</option>
        <option value="JAVA">JAVA</option>
        <option value="PHP">PHP</option>
        </select>
    <input type="button" value="show" onclick="choices()">
  </fieldset>
</form>
<div id="output"></div>

Sign up to request clarification or add additional context in comments.

3 Comments

Didn't my answer work? I wrote this answer 10 minutes ago than the others. And it's the same?
However, maybe the OP may have thought that the other answer provided them with more information (kind of ironic to their post though, lol!), I don't know; I can't speak for them.
No problem, your upvotes are more valuable than his accepting right now. He needs to read some rules about using here and why people are giving their precious time to answer questions like this. There are people to help to, and not sometimes.
1

No need for var choice = y.value;... it is never going to be able to tell you all the choices. As you can see from the rest of your code when you print that variable it only contains the first selection.

Instead just replace:

x.innerHTML += "the language is, " + choice + "<br>";

with

x.innerHTML += "the language is, " + current.value + "<br>";

You are already checking each option for whether it is selected, so simply output the value of each option.

Cheers!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.