0

I have vars that look like this:

var cardholder = $("#cardholder");
var cardholderInfo = $("#cardholder-Info");

And a function (which doesn't currently work) that looks like 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 I access like:

cardholder.keyup(validateRequired(cardholder));

I've looked everywhere but I can't find what I need and I'm not really sure what I should be searching for.

I can use the field value to access the straight cardholder var. But I also want to use the field value to then reference cardholderInfo so I can manipulate that element in the function.

4
  • 1
    Either use two arguments and pass in the cardholderInfo, or make use of javascripts lexical scoping and just use cardholderInfo in your function, if the function is defined in the same scope as cardholderInfo or in a scope which is further down the scope chain starting from cardholderInfo. Commented May 8, 2013 at 14:51
  • Let's get this straight: what DO you need? I think there is a much easier way of what you are trying... Commented May 8, 2013 at 14:53
  • @pythonian29033 Please please please never alert, learn how to use the console and log the object with console.log(field) in Chrome and Firefox with firebug you can click through the logged object to inspect it's properties. Commented May 8, 2013 at 14:58
  • and IMO, console.dir is good for firefox. Commented May 8, 2013 at 15:16

4 Answers 4

3

You would call the function like this, passing the second parameter:

cardholder.keyup(function () {
    validateRequired(this, cardholderInfo);
});

And modify your function to take a second parameter:

function validateRequired(field, fieldInfo){
    /* validation stuff */
}
Sign up to request clarification or add additional context in comments.

4 Comments

well spotted son! crap why didn't I see that?
this in keyup is not a jquery object so trying to do .val() on that will break the code.
@HMR, you are correct, and knowing this the validation function would have to turn that into a jQuery object if it wants to use .val() (perhaps with $(field)). My answer is showing how to pass the values to the function (which is what was asked), not actually do the validation.
It's a good answer, just a heads up to the OP that it'll break the validateRequired function unless called like: validateRequired($(this), cardholderInfo);
2

No need for the global variables:

function validateRequired($cardInfo){
    // You can guess what $cardInfo is
    //if it's NOT valid
    if(this.val().length < 1){
        this.addClass("field_error");
        $cardInfo.text("(Required)");
        $cardInfo.addClass("error");

        return false;
    }
    //if it's valid
    else{
        this.removeClass("field_error");
        $cardInfo.text("");
        $cardInfo.removeClass("error");
        return true;
    }
}


$(document).ready(function(){
  $("#cardholder").keyup(function(){
     validateRequired.call($(this),$("#cardholder-Info"));
  });
});

2 Comments

edit your code in the validate function, it's more erronious than the code in question
Thanks, didn't see the difference between field and fieldinfo. changed the code
1

Don't call the function you want to bind! If you need to pass an argument to it every time it is called, you either need to use bind or a function expression:

cardholder.keyup(functio(e) {
    return validateRequired(cardholder, cardholderInfo);
});

Also you will need a second parameter in your validateRequired function to get the fieldInfo variable filled:

function validateRequired(field, fieldInfo){
    …

Comments

1

You have to pass reference of function in keyup, you do not have to call function

cardholder.keyup(function(){
    validateRequired(cardholder)
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.