jQuery plugin, return value from function

Posted by Marius on Stack Overflow See other posts from Stack Overflow or by Marius
Published on 2010-04-11T17:26:02Z Indexed on 2010/04/11 17:33 UTC
Read the original article Hit count: 331

Filed under:
|
|
|

Hello there,

Markup:

<input type="text" name="email" />

Code:

$(':text').focusout(function(){
    $(this).validate(function(){
        $(this).attr('name');
    });
});

Plugin:

(function($){  
    $.fn.validate = function(type) {  
        return this.each(function(type) {  
            if (type == 'email') {
                matches = this.val().match('/.+@.+\..{2,7}/');
                (matches != null) ? alert('valid') : alert('invalid');
            }
            /*else if (type == 'name') {

            }
            else if (type == 'age') {

            }
            else if (type == 'text') {

            }*/
            else {
                alert('total failure');
            }
        });
    };  
})(jQuery);

The problem is that when I execute the code above, it runs the plugin as if type was a string: "function(){ $(this).attr('name'); });" instead of executing it as a function. How do I solve this?

Thank you for your time.

Kind regards, Marius

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about function