How to format a reusable code from normal jquery code -
i having function repeated on in site
jquery
$(function() { //check enabled state on load if(!$(".notifyowner").is(':checked')){ $(".ownerday").attr("disabled", "disabled"); $(".ownerdaytype").attr("disabled", "disabled"); } //toggle enabled state when checkbox clicked $(".notifyowner").click(function() { if($(this).is(":checked")) { $(".ownerday").removeattr("disabled"); $(".ownerdaytype").removeattr("disabled"); $(".owneractive").removeclass("disabled"); } else { $(".ownerday").attr("disabled", "disabled"); $(".ownerdaytype").attr("disabled", "disabled"); $(".owneractive").addclass("disabled"); } }); });
i tried make reusable code below
function selecttoggle(obj, dayclass, daytypeclass, txtactive){ var $event = $(obj); if(!($event).is(":checked")) { $(dayclass).attr("disabled", "disabled"); $(daytypeclass).attr("disabled", "disabled"); } $event.click(function() { if($event.is(":checked")) { $(dayclass).removeattr("disabled"); $(daytypeclass).removeattr("disabled"); $(txtactive).removeclass("disabled"); } else { $(dayclass).attr("disabled", "disabled"); $(daytypeclass).attr("disabled", "disabled"); $(txtactive).addclass("disabled"); } }); }
and calling onclick="selecttoggle(this,'ownerday','ownerdaytype','owneractive')"
but not working. mistake in script.
you don't call jquery select class. i.e. 'ownerday' should '.ownerday' in call. same others.
Comments
Post a Comment