0

Hi just wanted to know if this is a correct way. I know we can pass data using object notation and use it. But just wanted to know if the below code is correct. I am not sure about the below syntax.

 function isValid(errMessage,errId,errClass){
    alert();
    if($("#"+errId).val()==""){
        $("."+errClass).show();
        $("."+errClass).html(errMessage);
        return false;
    }else{
        $("."+errClass).hide();
    }
}

$("#fname").on("blur",isValid("please enter first name","fname","ferror"));

I have seen syntax like below:

function test(){
    alert("Hello World");
} 
$("#fname").on("blur",test);

check this link for output http://jsfiddle.net/susheel61/M2jsX/

so just want to make clear if passing parameters directly works or not.

1
  • Your jsfiddle uses a function call not a reference to a function. jsfiddle.net/M2jsX/1 Commented May 28, 2013 at 13:48

2 Answers 2

3

Your code call isValid method and assign result as blur handler so it's not exaclty right. You should do something like this

$("#fname").on("blur",function(){
    isValid("please enter first name","fname","ferror")
});

It seems there are other improvements you could do. The most obvious for me is pass this.id instead of harcoding the id

$("#fname").on("blur",function(){
    isValid("please enter first name", this.id, "ferror")
});
Sign up to request clarification or add additional context in comments.

5 Comments

yeah I know this one. But I have seen syntax like: function test(){ alert("Hello World"); } $("#fname").on("blur",test); so just want to make clear if passing parameters directly works or not
@susheel: I'm afraid I'm not sure what's your question. Your code as it is doesn't work. You have 2 working alternatives here.
I think my syntax is not valid. Just wanted to know if that exists. thanks.
@susheel: notice that instead of attaching method to blur is executing it. So in short, it's not working as you would expect.
yep. So I think I should pass as object or else follow your way :)
1

You can pass arguments to the event handler as a parameter to your event listener:

$("#fname").on("blur", {
        msg: "please enter first name", 
        name: "fname", 
        error: "ferror"
    }, isValid);

Then your isValid function can handle the data from the event object:

function isValid (event) {
    var errMessage = event.data.msg,
        errId = event.data.name,
        errClass = event.data.error;
    if ($("#"+errId).val()=="") {
        $("."+errClass).show();
        $("."+errClass).html(errMessage);
        return false;
    } else {
        $("."+errClass).hide();
    }
}

3 Comments

$("#fname").on("blur",isValid("please enter first name","fname","ferror")); Other options I knew. Just wanted to know if this syntax is legid. thanks
@susheel it is not. your function call yeilds a result - isValid() and the result is undefined. In this way you provide as a parameter an undefined event handler.
function test(){ alert("Hello World"); } $("#fname").on("blur",test); this syntax works So just thought even parameter thing should work. But I was wrong. Anyways thanks :)

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.