1

I have a div that is hidden when the DOM is loaded, using hide().

    <div class="disappear">
    Some text appears here
    </div>

How do I make the above div (.disappear) show when the input fields with class .master (one to three) are not empty. (Ideally, trigger it on .blur in each of the input fields.)

    <div id="form">
    <form>
    <input type="text" name="one" class="master">
    <input type="text" name="two" class="master">
    <input type="text" name="three" class="master">
    <input type="text" name="four" class="slave">
    <input type="text" name="five" class="slave">
    </form>
    </div>

Thanks in advance!

4 Answers 4

3

Something like this, I think:

$('.master').change(function() {
    if ($('.master').filter(function() {
        return !this.value;
    }).length) {
        // some are empty
        $('.disappear').hide();
    } else {
        // none are empty
        $('.disappear').show();
    }
});

jsFiddle. Note that I've used change rather than blur, because it's semantically what you mean and it's going to be faster.

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

2 Comments

@Stroller Glad to help. If an answer has resolved your question, you can mark it as accepted.
There seems to be a 5-minute gap before I can mark it as accepted. Will do as soon as it expires.
1

You would be doing it inside some event handler.

function YourEventHandler()
{
  var v1=$.trim($("#one").val());
  var v2=$.trim($("#two").val());
  var v3=$.trim($("#three").val());
  if(v1 !== '' && v1 !== undefined && v2 !== '' && v2 !== undefined v3 !== '' && v3 !== undefined)
  $(".disappear").show();
  else $(".disappear").hide();
}

I haven't used the .master as you might have that class in other elements. If not you can use that too. Give it a try & let me know.

Comments

1

try this ( this will also check for emptyness )

$('input.master').live('blur change', function () {
    var trimVal = $.trim($(this).val());
    if(!trimVal.length)
    {
        $('.disappear').show();
    }
    else {
         $('.disappear').hide();
    }   
});

working DEMO

Comments

0

This jsfiddle should do the job http://jsfiddle.net/dxpbT/

$(".master").blur(function() {
    if ($(this).val().length > 0) {
        var $all = $('.master');
        var $empty = $all.filter('[value=""]');

        if ($empty.length == 0) {
            $(".disappear").show();
        }    
    }

});

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.