Skip to content

Instantly share code, notes, and snippets.

@jimjeffers
Created March 29, 2010 06:07
Show Gist options
  • Save jimjeffers/347490 to your computer and use it in GitHub Desktop.
Save jimjeffers/347490 to your computer and use it in GitHub Desktop.
jQuery.fn.formtasticCustomRadios: (options) ->
defaults: {
activeClass: "checked"
inactiveClass: "unchecked"
customClass: false
defaultClass: "custom_radio"
rowClass: false
spanClass: "radio_button_element"
}
settings: jQuery.extend(defaults,options)
this.each( ->
row: $(this)
row.addClass(settings.rowClass) if settings.rowClass
row.find('label').each( ->
label: $(this)
# Append an anchor to label element.
label.after("<a href=\"#\" class="+settings.defaultClass+"></a>")
# Transpose HTML contents to custom element.
custom: label.next().html(label.html()).append("<span class="+settings.spanClass+"></span>")
label.html("").hide()
# Check radio input on click.
custom.click( (e) ->
target: $(e.target)
target.find("input[type=radio]").attr('checked', true);
false
)
)
)
custom_radios: this.find("a."+settings.defaultClass)
custom_radios.click( (e) ->
current: $(e.target)
custom_radios.removeClass(settings.activeClass).addClass(settings.inactiveClass)
current.removeClass(settings.inactiveClass).addClass(settings.activeClass)
)
(function(){
jQuery.fn.formtasticCustomRadios = function formtasticCustomRadios(options) {
var custom_radios, defaults, settings;
defaults = {
activeClass: "checked",
inactiveClass: "unchecked",
customClass: false,
defaultClass: "custom_radio",
rowClass: false,
spanClass: "radio_button_element"
};
settings = jQuery.extend(defaults, options);
this.each(function() {
var row;
row = $(this);
if (settings.rowClass) {
row.addClass(settings.rowClass);
}
return row.find('label').each(function() {
var custom, label;
label = $(this);
// Append an anchor to label element.
label.after("<a href=\"#\" class=" + settings.defaultClass + "></a>");
// Transpose HTML contents to custom element.
custom = label.next().html(label.html()).append("<span class=" + settings.spanClass + "></span>");
label.html("").hide();
// Check radio input on click.
return custom.click(function(e) {
var target;
target = $(e.target);
target.find("input[type=radio]").attr('checked', true);
return false;
});
});
});
custom_radios = this.find("a." + settings.defaultClass);
return custom_radios.click(function(e) {
var current;
current = $(e.target);
custom_radios.removeClass(settings.activeClass).addClass(settings.inactiveClass);
return current.removeClass(settings.inactiveClass).addClass(settings.activeClass);
});
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment