Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switching an image using jQuery

Tags:

jquery

image

Is there a better, more jQuery-ish way of handling this image substitution?

var image = $(obj).children("img");
if ($(image).attr("src") == "Images/TreeCollapse.gif")
   $(image).attr("src", "Images/TreeExpand.gif");
else
   $(image).attr("src", "Images/TreeCollapse.gif");
like image 494
Jon Tackabury Avatar asked Dec 04 '08 15:12

Jon Tackabury


1 Answers

Why set a variable when it isn't needed?

$(obj).children("img").toggle(
  function(){ $(this).attr("src", "Images/TreeExpand.gif"); },
  function(){ $(this).attr("src", "Images/TreeCollapse.gif"); }
);
like image 199
Josh Delsman Avatar answered Oct 04 '22 14:10

Josh Delsman