Skip to content

Instantly share code, notes, and snippets.

@ocharles
Created July 3, 2009 17:17
Show Gist options
  • Select an option

  • Save ocharles/140229 to your computer and use it in GitHub Desktop.

Select an option

Save ocharles/140229 to your computer and use it in GitHub Desktop.
jQuery.toggle = function(options)
{
var settings = {
on: function() { },
off: function() { },
on_graphic: '/static/images/release_editor/edit-on.png',
off_graphic: '/static/images/release_editor/edit-off.png',
initial: false,
};
jQuery.extend(settings, options);
var on = settings.initial;
var $img = $('<img>').attr('src', on ? settings.on_graphic : settings.off_graphic);
var $button = $('<button>').addClass('image-button').click(function(event) {
event.preventDefault();
on = !on;
if(on)
{
$img.attr('src', settings.on_graphic);
settings.on();
}
else
{
$img.attr('src', settings.off_graphic);
settings.off();
}
}).append($img);
return $button;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment