Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, how would you get the below to work exactly as it does but to also change a picture for each different selection e.g. 1 picture if you select 140mm x 300mm, a different picture if you select 140mm x 400mm and a different picture again if you select 140mm x 500mm? many thanks in advance.

<script language="JavaScript">

var DivTxt = new Array()
DivTxt[0] = "Please Select a Size to Show Price"
DivTxt[1] = "£10.00"
DivTxt[2] = "£11.00"
DivTxt[3] = "£12.00"
DivTxt[4] = "£13.00"
DivTxt[5] = "£14.00"
DivTxt[6] = "£15.00"
DivTxt[7] = "£16.00"
DivTxt[8] = "£17.00"
DivTxt[9] = "£18.00"
DivTxt[10] = "£19.00"
DivTxt[11] = "£20.00"
DivTxt[12] = "£21.00"
DivTxt[13] = "£22.00"
DivTxt[14] = "£23.00"
DivTxt[15] = "£24.00"
DivTxt[15] = "£25.00"

function getText(slction){
txtSelected = slction.selectedIndex;
document.getElementById('textDiv').innerHTML = DivTxt[txtSelected];
}
&lt;/script>


&lt;Select class="body_text" name="critical" >
Posted
Updated 14-May-15 5:39am
v2

1 solution

I'd be inclined to add the extra data as data- attributes on the <option> tags. That way, you keep the data together, and you won't forget to update the array when the list changes.
HTML
<select id="theList">
    <option value="" data-price="Please Select a Size to Show Price" data-image="http://placehold.it/150x150">--Select One--</option>
    <option value="1" data-price="£10" data-image="http://placehold.it/350x150">Item 1</option>
    <option value="2" data-price="£20" data-image="http://placehold.it/150x350">Item 2</option>
</select>

<p>Price: <span id="priceText"></span></p>
<p>Image: <img id="theImage" src="data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=" /></p>

<script>
var updateSelectedDetails = function(){
    var opt = $("#theList").find("option:selected");
    $("#priceText").text(opt.data("price"));
    $("#theImage").prop("src", opt.data("image"));
};

$("#theList").change(updateSelectedDetails);
updateSelectedDetails();
</script>

http://jsfiddle.net/2r606tar/[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900