0

I have a multiple selection box that I would like to use the value's from to display images based on selecting these values, eg. if one value is selected then one image would be displayed, if two are selected two would be displayed etc. I would also like a limit on it of three images displayed at once, no matter how many selections.

<select multiple name="item" class="details" action="post" id="mySelect">
<option value="one">One</option>
<option value="two">Two</option>
<option value="three">Three</option>
<option value="four">Four</option>
</select>

Any help would be greatly appreciated, thank you!

What I've tried so far:

<script type="text/javascript"> 
<!--  
function showimage() {  
var htmlStr=""; 
var option = document.getElementById("selectedValue"); 
for (i=0;i<option.options.length;i++) { 
if (option.options[i].selected) { 
htmlStr+='<img src="/products/"; 
htmlStr+=option.options[i].value; 
htmlStr+='" /><br />'; 
} 
} 
document.getElementById('mySelect').innerHTML = htmlStr; 
}  
//--> 
</script>

The images are located in /products/..

4
  • Where are the images? What have you tried so far? Commented Nov 8, 2014 at 15:59
  • You'll need JavaScript. Please try something and share your code when it doesn't work. We'll help you out! Commented Nov 8, 2014 at 15:59
  • @ThomasBormans I've added what I've tried above. It's a script I found online somewhere, which I've tried to edit to suit me. I'm not the best at javascript as I've only recently started learning it. Commented Nov 8, 2014 at 16:23
  • action attribute is for forms,not selects. Commented Nov 8, 2014 at 16:45

2 Answers 2

1

Don't forget to put correct image paths.

function imageFunc(imageid){
	
 var Imageplace=document.getElementById("myImage");
  Imageplace.src=imageid;
  }
<select multiple name="item" class="details"  id="mySelect" onchange="imageFunc(this.value)";>
<option value="1.jpg">One</option>
<option value="2.jpg">Two</option>
<option value="3.jpg">Three</option>
<option value="4.jpg">Four</option>
</select>
<img id="myImage" src="1.jpg"/>

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

Comments

0

Change your html to this:

<select multiple name="item" class="details" action="post" id="mySelect" onchange="showimage(this.value)">
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
    <option value="4">Four</option>
</select>

And your JavaScript to this:

function showimage(option) {
    var htmlStr="";
    if(option > 3) {
        option = 3;
    }
    for(i=0;i<option;i++) {
        htmlStr+='<img src="/products/' + i + '.jpg" /><br />';
    }
    document.getElementById('images').innerHTML = htmlStr;
}

All your images should be in the products folder with names: 1.jpg, 2.jpg, ...

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.