What is the best practice for including jQuery ext functions?

Posted by Metropolis on Stack Overflow See other posts from Stack Overflow or by Metropolis
Published on 2010-05-14T17:41:22Z Indexed on 2010/05/14 17:44 UTC
Read the original article Hit count: 211

Filed under:

Hey everyone,

Currently I have a file that I named JQuery.ext.js which I am including in all of my pages. Inside this file I have numerous functions that do things like the following,

(function($) {

    /**
     * Checks to see is the value inside ele is blank
     * @param message string The message that needs to be displayed if the element is blank
     * @return bool
     */
    $.fn.isEmpty = function(message) {
        var b_IsEmpty = false;

        //Loop through all elements
        this.each(function() {
            //Check to see if an empty value is found
            if($(this).val().length <= 0) {
                //If message is not blank
                if(message) {
                    alert(message);
                    $(this).focus().select();
                }

                b_IsEmpty = true;
                return false;
            }
            return true;
        });

        //Return false if the evaluation failed, otherwise return the jquery object so we can reuse it
        return (b_IsEmpty) ? true : false;
    };

    /**
     * Checks to see if the value inside ele is numbers only
     * @param message string The message that needs to be displayed if the element is not numeric
     * @return bool
     */
    $.fn.isNumeric = function(message) {
        var expression = /^[0-9]+$/;
        var b_IsNumeric = true;

        //Loop through elements checking each one
        this.each( function() {
            //Check to see if this value is not numeric
            if(!$(this).val().match(expression) && $(this).val().length > 0) {
                //If message is not blank
                if(message) {
                    alert(message);
                    $(this).focus().select();
                }

                b_IsNumeric = false;
            }
            return b_IsNumeric;
        });
        return b_IsNumeric;
    };
})(jQuery);

Is there another way to do this? or is this the way most people do it?

Thanks for any help, Metropolis

© Stack Overflow or respective owner

Related posts about jQuery