Search code examples
jqueryimagesrc

If Statement for Image src


How do i change this so that the #animation src only changes if its not already = suit.gif using an if statement can't figure out syntax.

<script type="text/javascript">
    $(function() {
      $("#nav").click(function() {
            $('html,body').animate({  
                scrollTop: $(document).scrollTop()+600
            }, 1000);
                $('#animation').attr('src','images/suit.gif');
      });
    });
</script>

Solution

  • Try this:

    $(function() {
      $("#nav").click(function() {
    
         $('html,body').animate({  
            scrollTop: $(document).scrollTop()+600
         }, 1000);
    
         if($('#animation').attr('src') != 'images/suit.gif'){
            $('#animation').attr('src','images/suit.gif');
         }
       });     
     });