nextlevelgamingCO
nextlevelgamingCO

Reputation: 71

change image on click jQuery

I have this jQuery slide code- EDIT-Sorry guys grabbed the wrong code :/ heres the actually code

<script type="text/javascript"> 

     $(document).ready(function() {
        $(".slidingDiv").hide();  
        $('.secondarytitle').click(function() {  
           $(this).closest('tr').siblings('.slidingDiv').slideToggle(); 
        });  
     });

</script>

I have to images for the click function, which will be a

<table>
  <tr>
    <th class="image1">Welcome</th>
  </tr>
  <tr>
    <td></td>
  </tr>
</table>

What I would like to do is when the object is clicked it changes the image, once clicked again it will change back to the original image. Any suggestions will work for me. Even if you suggest a different code to slide the content. Thanks guys

Upvotes: 1

Views: 3762

Answers (2)

Uttara
Uttara

Reputation: 2532

just a slight change in Anton's answer. you can use toggle() method not requiring to check img src

var imgSRC={'src1':'URL_1','src2': 'URL_2' }

$('.image1').toggle(function(){
      $(this).attr('src',imgSRC.src2)
   }, function() {
       $(this).attr('src',imgSRC.src1)
   }
});

Upvotes: 2

Anton Baksheiev
Anton Baksheiev

Reputation: 2251

Try this approach

var imgSRC={'src1':'URL_1','src2': 'URL_2' }

$('.image1').click(function(){
   if($(this).attr('src')==imgSRC.src1){
       $(this).attr('src',imgSRC.src2)
   }else{
       $(this).attr('src',imgSRC.src1)
   }
})

Upvotes: 3

Related Questions