Sunday, July 5, 2015

EXTENDED METHODS IN jQuery


/******* EXTENDED METHODS IN jQuery ***************/

/*Mehod1: this will remove any inline css attribute.  */
(function ($) {
    $.fn.removeStyle = function (style) {
        var search = new RegExp(style + '[^;]+;?', 'g');

        return this.each(function () {
            $(this).attr('style', function (i, style) {
                return style.replace(search, '');
            });
        });
    };
}(jQuery));

/*Mehod2: this will change existing class with red. pre-requites class name should be postfixed with _red with existing class name e.g. Existing class name .top_lt  so new name for redclass will be .top_lt_red
It will only work when setRedClass will set to 1 e.g. setRedClass(1)-> All class will renamed with redclass setRedClass(0)-> nothing will happen.
*/
(function ($) {
    $.fn.setRedClass = function (baddebtValue) {
        if (baddebtValue == "1") {
            $("[setBDColor='yes']").removeStyle('background');
            $("[setBDColor='yes']").css("background", 'url(../../images/box_hdg_bg_red.gif) left top repeat-x;');
            $("[setBDColor='redclass']").each(function (a, b, c) {
                var className = $(this).attr('class').trim();
                $(this).removeClass(className);
                className = className + '_red';
                $(this).addClass(className);
            });
        }
    };
}(jQuery));
//How to call
$('#abc').find('a').removeStyle('color');
Method2
 $.fn.setRedClass($("[id$=hfIsBaddebt").val());

No comments: