simple-html-radio
Version:
A jQuery plugin that helps in styling native radio buttons by hiding and replacing it with a span element that can be styled easily. The script gracefully degrades, tabbable, and supports keyboard navigation.
1 lines • 2.18 kB
JavaScript
(function(f){var a=32,b=37,l=39,h=38,k=40;var d={init:function(m){var n={classNames:{main:"simple-html-radio",inside:"simple-html-radio-inside",checked:"simple-html-radio-checked",disabled:"simple-html-radio-disabled",hidden:"simple-html-radio-hidden"}};m=f.extend(true,{},n,m);return this.each(function(){var q=f(this);if("input"!=q[0].nodeName.toLowerCase()||"radio"!=q.attr("type").toLowerCase()){f.error("Not a radio element on jQuery.simpleHtmlRadio")}else{var s=q.val(),o=0,p="",r=null;p='<span class="'+m.classNames.main+'"><span class="'+m.classNames.inside+'"></span></span>';r=f(p).insertAfter(q);r.data("radioObj",q);q.data("simpleHtmlRadio",r);if(q.attr("disabled")){r.addClass(m.classNames.disabled)}else{r.attr("tabindex","0");q.bind("click.simpleHtmlRadio",g);r.bind("click.simpleHtmlRadio",j);r.on("keydown",function(A){A=A||window.event;var u=f(this),y=null,w="",x=u.data("radioObj"),v=-1,t=0,z=-1;if(A.keyCode){y=A.keyCode}if(x){w=x.attr("name");v=f("input[name='"+w+"']").index(x);t=f("input[name='"+w+"']").length}if(y==a){f(this).trigger("click.simpleHtmlRadio");A.preventDefault()}else{if(y==b){e(v,t,w);A.preventDefault()}else{if(y==l){i(v,t,w);A.preventDefault()}else{if(y==k){i(v,t,w);A.preventDefault()}else{if(y==h){e(v,t,w);A.preventDefault()}}}}}})}if(q.is(":checked")){r.addClass(m.classNames.checked)}q.addClass(m.classNames.hidden)}})}};f.fn.simpleHtmlRadio=function(m){if(d[m]){return d[m].apply(this,Array.prototype.slice.call(arguments,1))}else{if(typeof m==="object"||!m){return d.init.apply(this,arguments)}else{f.error("Method "+m+" does not exist on jQuery.simpleHtmlRadio")}}return false};function g(p){var n=f(this),m=n.attr("name"),o=n.data("simpleHtmlRadio");f('input[name="'+m+'"]').each(function(){var q=f(this),r=q.data("simpleHtmlRadio");if(r){r.removeClass("simple-html-radio-checked")}});o.focus();o.addClass("simple-html-radio-checked")}function j(o){var n=f(this),m=n.data("radioObj");if(m){m.trigger("click.simpleHtmlRadio")}}function e(o,m,n){if(o!=-1){var p=o-1;if(p>=0){c(n,p)}}}function i(o,m,n){if(o!=-1){var p=o+1;if(p<=m){c(n,p)}}}function c(n,m){f("input[name='"+n+"']").eq(m).trigger("click.simpleHtmlRadio")}})(jQuery);