Carson
Carson

Reputation: 4651

Javascript - If drop down is selected, change image in a container

Hey guys. I'm trying to change the image in a container with what I thought was simple javascript, but can't seem to figure it out. "cover-image" is the container where the image is, and "txtMontage" is the ID of the drop down list. If I run this as is, no mater what I select the image is set by the first if statement, and then the drop down gets stuck on it so I can't select anything else.

Any ideas on how to fix this? Thanks.

<script type="text/javascript">
    function showPreview() {

        var image = document.getElementById("cover-image");
        var dropd = document.getElementById("txtMontage");

        if (dropd.value = "abrasives") {
            var container= "img/abrasives.jpg";
            image.src = container;
        }
        else if (dropd.value = "industrial") {
            var container= "img/gen-industrial.jpg";
            image.src = container;
        }

    }
</script>

Upvotes: 1

Views: 4091

Answers (3)

Rahul rajoria
Rahul rajoria

Reputation: 129

<script language="javascript">
function jsDropDown(imgid,folder,newimg){
document.getElementById(imgid).src = "http://mcxbazaar.com/Abazar_new/Abazar_html_them2/" + folder + "/" + newimg + ".jpg"; 

}


</script>

<div style="width:600px; height:300px;">
<div style="float:left; width:280px;"> <select class="input_select" name="products"   onchange="jsDropDown('rahul','images',this.value)">

                             <option value="PALLET-RACKING">Pallet Racking</option>
                          <option value="SLOTTED-ANGLE-RACKING">Slotted Angel</option>
                          <option value="BOLT-FREE">Bolts Free</option>
                          <option value="Pallet-Rack">Rack Supported Plateform</option>
                          <option value="PLASTIC-BINS">Plastic Bins</option>
                            </select></div>
        <div style="float:left; width:280px; height:300px">
        <img src="http://mcxbazaar.com/Abazar_new/Abazar_html_them2/images/PALLET-RACKING.jpg" width="300" height="300" id="rahul" ></div>
</div>

Upvotes: 0

Josh Mein
Josh Mein

Reputation: 28635

You need to change your if to say

 if (dropd.value == "abrasives")

In javascript = sets the value of an object and == is used for comparison

Upvotes: 1

Sarfraz
Sarfraz

Reputation: 382686

You are specifying = instead of == in your conditions:

Use:

if (dropd.value == "abrasives") {

Instead of:

if (dropd.value = "abrasives") {

Upvotes: 1

Related Questions