Parveen.gmp
Parveen.gmp

Reputation: 17

The script is not changing the image after once

The script is changing the image only once. Then it is not working at all.

SCRIPT:

<script type="text/javascript">
    function change_image(){
        if (window.document.pic.src='images/NAV_OFFhome_trans.png')
        {
            window.document.pic.src='images/NAV_OFFhist_trans.png';
        }
        else if (window.document.pic.src='images/NAV_OFFhist_trans.png')
        {
            window.document.pic.src='images/NAV_OFFhome_trans.png';
        }
    }
</script>

HTML:

<div id="main" >
<img src="images/NAV_OFFhome_trans.png" name="pic" onclick="change_image()"     style="position:absolute;"/>
</div>

Upvotes: 0

Views: 41

Answers (1)

Brian Poole
Brian Poole

Reputation: 711

In your if/else if statements, you're setting the src instead of checking against the value. Make sure to use == instead of =.

As such:

<script type="text/javascript">
    function change_image(){
        if (window.document.pic.src == 'images/NAV_OFFhome_trans.png')
        {
            window.document.pic.src = 'images/NAV_OFFhist_trans.png';
        }
        else if (window.document.pic.src == 'images/NAV_OFFhist_trans.png')
        {
            window.document.pic.src = 'images/NAV_OFFhome_trans.png';
        }
    }
</script>

Here's an example for good measure: http://jsfiddle.net/rd6dL/

Upvotes: 2

Related Questions