Ryan Stav
Ryan Stav

Reputation: 19

How to display an array value based on a selection from a select option box?

<!DOCTYPE html>
<html>
<body bgcolor="#999966">
 <select id="myFruitSelect">
    <option value="0"> Please Choose a Fruit </option>
    <option value="1"> Apple</option>
    <option value="2">Orange</option>
    <option value="3">Pineapple</option>
    <option value="4">Banana</option>
    <option value="5"> Watermelon </option>
  </select>
</form>
<br>
<button onclick="myFunction()"> Select Your Fruit</button>
<img id="outputImage" src=""/>

<script>
function myFunction() {
    var n =parseInt(x) -1;
    var pictures=[ "apple.png", "orange.png", "pineapple.png", "banana.png", "Watermelon.png"];
    var x = document.getElementById("myFruitSelect").value; 
    document.getElementById('outpu​tImage').src = pictures[n-1];
}
</script>
</body>
</html>

Any help or points in the right direction is appreciated

I'm trying to make a page that a user selects a option from the dropdown and an array value is then shown based on the user's selection. I can't figure out how to access the array value based on the user selection. I can only use HTML and Script so no answers with jQuery please.

Upvotes: 1

Views: 276

Answers (3)

David Florez
David Florez

Reputation: 1436

This is rare, apparently your code is ok but there is a difference between the id you are giving to your image element and the one on your document.getElementById both seems the same to me but when i copy and paste the id from the image to the document.getElementById() it works perfect.

Upvotes: 1

Rudresh Ajgaonkar
Rudresh Ajgaonkar

Reputation: 790

<!DOCTYPE html>
<html>
<body bgcolor="#999966">
<form>
 <select id="myFruitSelect">
    <option value="0"> Please Choose a Fruit </option>
    <option value="1"> Apple</option>
    <option value="2">Orange</option>
    <option value="3">Pineapple</option>
    <option value="4">Banana</option>
    <option value="5"> Watermelon </option>
  </select>
</form>

<br>

<button onclick="myFunction()"> Select Your Fruit</button>

<img id="outputImage" src=""/>



<script>
function myFunction() {

    var pictures=[ "apple.png", "orange.png", "pineapple.png", "banana.png", "Watermelon.png"];
     x = document.getElementById("myFruitSelect").value;
     //This will give you index of the selected option.
     //As the array starts from 0 , we need to subtract 1 to access the particular picture
     y = parseInt(x);
     m = y-1;
     console.log(pictures[m]);
    document.getElementById('outpu​tImage').setAttribute("src", pictures[m]);

    }
</script>

</body>
</html>

Upvotes: 0

Rob M.
Rob M.

Reputation: 36521

This line seems to be problematic (var n =parseInt(x) -1;), as you are using x before you defined it. You could just move that declaration below your var x declaration, or get rid of it altogether:

<!DOCTYPE html>
<html>
<body bgcolor="#999966">
 <select id="myFruitSelect">
    <option value="0"> Please Choose a Fruit </option>
    <option value="1"> Apple</option>
    <option value="2">Orange</option>
    <option value="3">Pineapple</option>
    <option value="4">Banana</option>
    <option value="5"> Watermelon </option>
  </select>
</form>
<br>
<button onclick="myFunction()"> Select Your Fruit</button>
<img id="outputImage" src=""/>

<script>
function myFunction() {
    var pictures= [ "apple.png", "orange.png", "pineapple.png", "banana.png", "Watermelon.png"];
    var x = document.getElementById("myFruitSelect").value; 
    document.getElementById('outputImage').src = pictures[parseInt(x, 10)-1];
}
</script>
</body>
</html>

Upvotes: 2

Related Questions