Lee S
Lee S

Reputation: 343

jQuery empty input check function not working as expected

This question has been done to death on SO and I'm really, really sorry! I've already taken the bones of the below idea from a couple of SO questions on the same theme.

All said though, I still can't get it to work as expected.

JSFIDDLE

$(document).ready(function (e) {
    // completed count submit handler
    $("#submit_counts_button").on('click', function () {
        window.incomplete = false;
        $('input[type=number]').each(function () {
            if ($(this).val().length == 0) {
                window.incomplete = true;
                alert('Some fields are empty');
                return false;
            } else {
                if (window.incomplete === false) {
                    $("#submit_counts_button").prop('disabled', true);
                    $("#submit_counts_button").html('Please Wait ...');
                    //$("#update_form").submit();
                }
            }
        });

    });
});

I'm sure it's something totally embarrassingly obvious but after a 16 hour day, I just can't see it. Any help appreciated ...

Upvotes: 0

Views: 43

Answers (2)

Kaizen Programmer
Kaizen Programmer

Reputation: 3818

You need to pull the 'incompletion' check outside of the .each

$(document).ready(function (e) {
    // completed count submit handler
    $("#submit_counts_button").on('click', function () {
        window.incomplete = false;
        $('input[type=number]').each(function () {
            if ($(this).val().length == 0) {
                window.incomplete = true;
                alert('Some fields are empty');
                return false;
            }
        });

        if (window.incomplete === false) {
            $("#submit_counts_button").prop('disabled', true);
            $("#submit_counts_button").html('Please Wait ...');
            //$("#update_form").submit();
        }
    });
});

http://jsfiddle.net/6WpeF/6/

Upvotes: 1

ThatGuy343
ThatGuy343

Reputation: 2424

try

if(document.getElementById('id of input').value != ""){}

Upvotes: 0

Related Questions