Pure HTML / CSS
http://jsbin.com/jQuery_Toggle/5/edit
<label class="tog"> <input type="checkbox"> <span></span> </label>
label.tog > input{ display:none; } label.tog > span{ display:inline-block; width:16px; height:16px; background: url("play.png"); } label.tog > input:checked + span{ background-image: url("pause.png"); }
Toggle Internal Span Images
Useful reason for not loading a new request onto the server:
http://jsbin.com/jQuery_Toggle/4/edit
<span class="tog"> <img src="play.png" /> <img style="display:none;" src="pause.png" /> </span>
$(".tog").click(function(){ $('img',this).toggle(); });
Or, say, we have this HTML and a .tog
selector:
<img class="tog" src="play.png"/>
Using Array.prototype.reverse ()
http://jsbin.com/jQuery_Toggle/1/edit
var togSrc = [ "play.png", "pause.png" ]; $(".tog").click(function() { this.src = togSrc.reverse()[0]; });
Using the current src
value and String.prototype.match()
Useful if you do not know the initial state (play? Pause?)
http://jsbin.com/jQuery_Toggle/3/edit
var togSrc = [ "play.png", "pause.png" ]; $(".tog").click(function() { this.src = togSrc[ this.src.match('play') ? 1 : 0 ]; });
NOTE. for the last two examples, you need to preload the images in order to prevent the time that browsers take to download a new image from the server.
source share