2

I have a form with a few input fields, I only want to show a div when all the input fields got content, when one of the input fields has no content the div should disappear again.

I made it work with one input field, but how do I get it to work when all the input fields are filled in (don't know if its a good clean way?):

$(function () {    
$('input').change(function() {
$('.next').toggle($(this).val().length !== 0);
}); });

Fiddle:
http://jsfiddle.net/uQyH9/19/

3 Answers 3

2

Try this : http://jsfiddle.net/uQyH9/21/

$(function () {   
    var _cached=$('input');

    _cached.change(function() {
        if (_cached.filter(function (){return $(this).val().length }).length==_cached.length)
        $('.next').show();
         else
        $('.next').hide();

    });
});
Sign up to request clarification or add additional context in comments.

5 Comments

Ummm.. I'm sorry but I'm kind of curious, what's the _ stands for, is it just like a variable name?
@WesleyLachenal caching the variable instead of re-calc each time
Yeah I'd never name a variable _. Completely non-intuitive.
@AndersG no . the var is on it's own scope.
No critique intended, just a simple question :-)
1

You can use a filter function to check that all the input are filled.

Code:

    $(function () {
        $('input').change(function () {
            $('.next').toggle($("input").filter(function () {
                return this.value === "";
            }).length === 0)
        });
    });

Demo: http://jsfiddle.net/IrvinDominin/DwF2P/

UPDATE

You can check the value of the elements by type by cheking type attribute.

Code:

$(function () {
    $('input').change(function () {
        $('.next').toggle($("input").filter(function () {
            var myType=$(this).attr("type");
            if (myType === "checkbox") return !$(this).is(":checked");
            if (myType==="radio"){
                var myName = $(this).attr("name");
                if (myName==="") return !$(this).is(":checked");
                return $('input[name='+ myName +']:checked').length===0
            }
            return this.value === "";
        }).length === 0)
    });
});

Demo: http://jsfiddle.net/IrvinDominin/pqJhg/

2 Comments

This one works best! But I have one thing to add that I did forgot to mention; is it also possible when one of the input fields is a radio button? See: jsfiddle.net/DwF2P/1
@ErwinvanEkeren you are welcome; for different types check the edit
0

Loop over the inputs. If you find one that isn't filled in, then hide the DIV. If you don't, show the DIV.

$('input').change(function() {
    var allFilled = true;
    $('input').each(function() {
        if (this.value === '') {
            allFilled = false;
            return false; // Terminate the loop
        }
    }
    $('.next').toggle(allFilled);
});

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.