Miomir Dancevic
Miomir Dancevic

Reputation: 6852

Changing image inside element?

I haveve some function like this

 $(document).ready(function(){
$('.close').click(function(){
     $(this).next(".firsttableheading").slideToggle();

     if($(this).text() == "-" ? $(this).text("+") : $(this).text("-"));
});     
 });

And html is

<a href="#" class="close" onClick="return false">

Evrything working ok, but i got problem when i want to put picture inside that a href, and change it on click

example i have image1.jpg and image2.jpg

 $(document).ready(function(){
$('.close').click(function(){
     $(this).next(".firsttableheading").slideToggle();

     if($(this).img()  == "image1.jpg" ? $(this).img("image2.jpg") : $(this).img("image1.jpg"));
});     
 });

It is not working maybe i didnt get the element ok?

Upvotes: 0

Views: 71

Answers (1)

Khanh TO
Khanh TO

Reputation: 48972

$(document).ready(function(){
    $('.close').click(function(){
         $(this).next(".firsttableheading").slideToggle();

         var img = $(this).find("img");
         img.attr("src")  == "image1.jpg" ? img.attr("src","image2.jpg") : img.attr("src","image1.jpg");
        });     
  });

Or

$(document).ready(function(){
        $('.close').click(function(){
             $(this).next(".firsttableheading").slideToggle();

             var img = $(this).find("img");
             img.attr('src', function(i,s){ 
                               return s == 'image1.jpg' ? 'image2.jpg' : 'image1.jpg'; });
            });     
      });

Upvotes: 1

Related Questions