Skip to content

Commit

Permalink
* jQuery Radiobutton v2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
tomekwojcik committed Mar 10, 2012
1 parent e03c5c7 commit db9ef36
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 61 deletions.
61 changes: 0 additions & 61 deletions jquery-radiobutton-1.0.js

This file was deleted.

69 changes: 69 additions & 0 deletions jquery-radiobutton-2.0.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* jQuery custom radiobuttons
*
* Copyright (c) 2010-2012 Tomasz Wójcik (bthlabs.pl)
* Licensed under the MIT License:
* http://www.opensource.org/licenses/mit-license.php
*
* @version 2.0
* @category visual
* @package jquery
* @subpakage ui.checkbox
* @author Tomasz Wójcik <labs@tomekwojcik.pl>
*/
(function() {
jQuery.fn.radiobutton = function(options) {
options = options || {};

var defaults = {
className: 'jquery-radiobutton',
checkedClass: 'jquery-radiobutton-on'
};

var settings = jQuery.extend(defaults, options || {});

return this.each(function() {
var self = $(this);

var replacement = jQuery(
'<div class="' + settings.className + '-wrapper">' +
'<a class="' + settings.className + '" href="#" name="' + self.attr('id') + '" rel="' + self.attr('name') + '"></a>' +
'</div>'
);
var element = jQuery('a', replacement);

if (self.attr('checked') === 'checked') {
element.addClass(settings.checkedClass);
}

element.on('click', function(event) {
event.preventDefault();
event.stopPropagation();

var input = jQuery('input#' + jQuery(this).attr('name'), replacement.parent());
if (input.attr('checked') === 'checked') {
input.removeAttr('checked');
} else {
input.attr('checked', 'checked');
}
input.trigger('change');

return false;
});

self.on('change', function(event) {
var input = jQuery(this);
jQuery('a[rel="' + input.attr('name') + '"].' + settings.checkedClass).removeClass(settings.checkedClass);

if (input.attr('checked') === 'checked') {
jQuery('a[name=' + input.attr('id') + ']', replacement.parent()).addClass(settings.checkedClass);
} else {
jQuery('a[name=' + input.attr('id') + ']', replacement.parent()).removeClass(settings.checkedClass);
} // eof if()
});

self.css({ 'position': 'absolute', 'top': '-200px', 'left': '-200px'}).before(replacement);
replacement.parent().css('overflow', 'hidden');
});
};
})();

0 comments on commit db9ef36

Please sign in to comment.