views:

31

answers:

2

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>
+2  A: 

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

Use:

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

Instead of:

if (dropd.value = "abrasives") {
Sarfraz
Such a common noob mistake - assignment instead of comparison. +1
Jacob Relkin
Wow.. you really do forget the little things when you haven't used JS in a while.Thanks.
Carson
A: 

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

jmein