0

Hi I am trying make a search button active/inactive based on whether the user has entered text into BOTH textboxes. Currently it is becoming active when the user enters text on only the first box. Can anyone please tell me what I am doing wrong?? Thanks in advance!

$(".searchBtn.active").hide();
$(".searchBtn.inactive").prop('disabled', true);

$.each($(".searchFormContent input[type=text]"), function () {
     $(this).keyup(function () {
         if ($(this).val().length > 0) {
             $(".searchBtn.inactive").hide();
             $(".searchBtn.active").show();
         } else {
             $(".searchBtn.active").hide();
             $(".searchBtn.inactive").show();
         }
     });
});

I have now tried this:

$(".searchFormContent input[type=text]").each(function () {
            $(this).keyup(function () {
                if ($(this).val().length > 0) {
                    $(".searchBtn.inactive").hide();
                    $(".searchBtn.active").show();
                } else {
                    $(".searchBtn.active").hide();
                    $(".searchBtn.inactive").show();
                }
            })
        });

and this:

$(".searchFormContent input[type=text]").keyup(function () { if ($(this).val().length > 0) { $(".searchBtn.inactive").hide(); $(".searchBtn.active").show(); } else { $(".searchBtn.active").hide(); $(".searchBtn.inactive").show(); } });

Getting the same result every time.

1 Answer 1

2

When looping over the results of a selector, use the method syntax:

$(".searchFormContent input[type=text]").each(...);

The syntax you used is for looping over an array or object.

See the following documentation:

However, you don't need to use .each() when binding event handlers, the event handler methods operate on a selector directly:

$(".searchFormContent input[type=text]").keyup(function() ...);

To solve the problem you're having, you need to loop inside the event handler:

$(".searchFormContent input[type=text]").keyup(function() {
    var allFilled = true;
    $(".searchFormContent input[type=text]").each(function() {
        if (this.value == '') {
            allFilled = false;
            return false; // End the loop
        }
    });
    $(".searchBtn.inactive").toggle(!allFilled);
    $(".searchBtn.active").toggle(allFilled);
});
Sign up to request clarification or add additional context in comments.

5 Comments

You missed a double quote, I couldn't edit just one character.
Thank you for the input but that doesn't fix the problem.
I am getting the same result with or without the .each
That's because you're only testing the current input in your event handler. I've added code that loops over all the inputs to the answer.
WOW thank you so much for the quick responses, your code works perfectly!

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.