Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
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. –  Johannes Lumpe May 8 '13 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... –  Zyrius May 8 '13 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. –  HMR May 8 '13 at 14:58
    
and IMO, console.dir is good for firefox. –  Mr_Green May 8 '13 at 15:16

4 Answers 4

up vote 3 down vote accepted

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 */
}
share|improve this answer
    
well spotted son! crap why didn't I see that? –  pythonian29033 May 8 '13 at 14:53
    
this in keyup is not a jquery object so trying to do .val() on that will break the code. –  HMR May 8 '13 at 15:03
    
@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. –  Derek Henderson May 8 '13 at 15:07
    
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); –  HMR May 8 '13 at 15:12

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"));
  });
});
share|improve this answer
    
edit your code in the validate function, it's more erronious than the code in question –  pythonian29033 May 8 '13 at 14:57
    
Thanks, didn't see the difference between field and fieldinfo. changed the code –  HMR May 8 '13 at 15:02

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){
    …
share|improve this answer

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

cardholder.keyup(function(){
    validateRequired(cardholder)
});
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.