1
<!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.

3 Answers 3

2

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>

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

3 Comments

It's worth noting that the reason this didn't throw an error was because of hoisting.
How can I also have a div display and the picture display once I select an option? forgot to add that but it does work :)
@RyanStav Use onchange on the <select>
1

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.

Comments

0
<!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>

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.