jquery - Javascript: Combine function argument with string to reference var -
i have vars this:
var cardholder = $("#cardholder"); var cardholderinfo = $("#cardholder-info");
and function (which doesn't work) looks this:
function validaterequired(field){ //if it's not valid if(field.val().length < 1){ field.addclass("field_error"); fieldinfo.text("(required)"); fieldinfo.addclass("error"); return false; } //if it's valid else{ field.removeclass("field_error"); fieldinfo.text(""); fieldinfo.removeclass("error"); return true; } }
which access like:
cardholder.keyup(validaterequired(cardholder));
i've looked everywhere can't find need , i'm not sure should searching for.
i can use field
value access straight cardholder
var. want use field
value reference cardholderinfo
can manipulate element in function.
you call function this, passing second parameter:
cardholder.keyup(function () { validaterequired(this, cardholderinfo); });
and modify function take second parameter:
function validaterequired(field, fieldinfo){ /* validation stuff */ }
Comments
Post a Comment