Skip to content

Instantly share code, notes, and snippets.

@jshaw
Created March 8, 2013 19:37
Show Gist options
  • Save jshaw/5119184 to your computer and use it in GitHub Desktop.
Save jshaw/5119184 to your computer and use it in GitHub Desktop.
Optimized image swapping. jQuery required
.button {
color: blue;
text-decoration: underline;
cursor: pointer;
}
#img1,
#img2 {
float: left;
margin: 5px;
}
<p class="button swapimage_ele">swap image element</p>
<p class="button swapimage_src">swap image src</p>
<br />
<div id="img1">
<img src="http://farm8.staticflickr.com/7244/7339763896_6b3a7487fe_s.jpg" />
</div>
<div id="img2">
<img src="http://farm9.staticflickr.com/8016/7339765048_d83ffbb15d_s.jpg" />
</div>
$('.swapimage_ele').on("click", function(evt){
var img1 = $("#img1").html();
var img2 = $("#img2").html();
$("#img1").html(img2);
$("#img2").html(img1);
});
$('.swapimage_src').on("click", function(evt){
var img1 = $("#img1 img").attr("src");
var img2 = $("#img2 img").attr("src");
$("#img1 img").attr("src", img2);
$("#img2 img").attr("src", img1);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment