1

I wish to count the number of textboxes that's been filled. I have the following code but it's not working the way I want it too. I'm new to JQuery and the script is based from my research here on Stack Overflow.

Markup:

<input class="a" type="text" name="rep">
<input class="a" type="text" name="rep">
<input class="a" type="text" name="rep">

Script:

$(".a").blur(alert(countRep()));

function countRep() {
    var fieldTxtbx = $(".a").filter(function(){
        return $.trim($(this).val()) != '';
    }).length;
    return fieldTxtbx;
}

Why does it work the on load but everytime I input in the textbox the blur() is not called?

4 Answers 4

2

Currently you are binding the return value of countRep() with blur event which is not a function.

bind a function with event and call your method.

$(".a").blur(function() {
    alert(countRep())
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I have another question, if it's okay, now I'm using it in a condition but why doesn't it return any value? $('[name=attendance]:checked').length + countRep >= 4)
0

Change this:

$(".a").blur(alert(countRep()));

With this:

$(".a").blur(alert(countRep)); // no needed to use parenthesis to call function.

Or, use like this:

$(".a").blur(function(){
  alert(countRep());
});

Comments

0

Modify Your blur event to:

$(".a").blur(function(){
 alert(countRep());
});

also you can use narrow down selector for non empty textboxes to:

$('.a').filter(function() {
   return $(this).val(); 
});

Comments

0

Try the following instead of the first line:

'$(".a").on("blur", OnBlur);

function OnBlur(event) {
  alert(countRep);
}

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.