Reputation: 35180
fn to create functions/methods before without any hassle, however, I'm now having some issues. Any help would be greatly appreciated :)
$.fn.formClass = function(class)
{
var bad_class = (class=='input_good') ? 'input_bad' : 'input_good';
if($(this).hasClass(bad_class))$(this).removeClass(bad_class);
$(this).addClass(class);
}
and I'm trying to use it inside this function:
function check_username()
{
var username = $('input[name="username"]');
if(username.val().length<4||username.val.length>20)
{username.formClass('input_bad');}
}
The Console.log is saying Uncaught TypeError: Object [object Object] has no method 'formClass'
Thanks is advance for any help!
Upvotes: 1
Views: 1822
Reputation: 3732
I believe you should be getting an error about using the variable name class
since it is a reserved keyword. Try renaming it to something else:
$.fn.formClass = function(class_name) {
var bad_class = (class_name=='input_good') ? 'input_bad' : 'input_good';
if($(this).hasClass(bad_class))$(this).removeClass(bad_class);
$(this).addClass(class_name);
}
Upvotes: 6