0

I want to check if the input is empty or not while the user is typing

this is my code

$('#visitID').on('change', function () {
        if ($('#visitID').val().length == 0) {
            $("#saveandclosebutton").hide();
        } else {
            $("#saveandclosebutton").show();
        }
    });

but the saveandclosebutton never becomes hidden even though if my input is empty.

1
  • 1
    try keypress/keyup events because now your code will fire only when the input loses its focus after changing the value Commented Mar 31, 2014 at 16:00

6 Answers 6

3

You could include checking on keyup as well, e.g:

$('#visitID').on('change keyup', function () {
  this.value ? $("#saveandclosebutton").show() : $("#saveandclosebutton").hide();
});

jsFiddle here

Of course you'd need to set the button to be display:none initally, as your <input> would originally be empty.

#saveandclosebutton {
   display:none;
}
Sign up to request clarification or add additional context in comments.

2 Comments

still the button is always shows. could you give an jidffl example ?
Thanks for your answer, but that guy has helped me first. I wish I could accept more than one answer, +1 to your efforts with me. you great
1

Bind the event handler to both the keyup and change events, but you also want to cause the event handler to get executed when the page loads:

$('#visitID').on('keyup change', function() {
    $('#saveandclosebutton').toggle($(this).val().length > 0);
}).change();

jsfiddle

2 Comments

The first code you provided was better except that it shows the button when the input is empty and hide the button when the input is fill. your code now is hide then show then hide then show with each new input
@user2226785 - The issue was that the argument to the .toggle() method must be a boolean. I have edited my answer to fix the code.
1

I guess that the saveandclosebutton is called differently. Try first to see if the main logic is working at all:

$('#visitID').on('change keyup', function () {
      console.log($('#visitID').val().length);
});

and check if the console has any output at all. You can add the keyup as I did so that the check is done after each keypress.

2 Comments

Do you imply that the script is not being called? I have 45 other scripts on that file and all are working. So, I guess the function is executed.
Well, no need to feel offended, it is the first step in debugging. Check if the function is called at all. You could also use the debugger; statement.
0

Try .toggle( showOrHide )

$('#visitID').on('change keyup', function () {
    $("#saveandclosebutton").toggle(this.value.length == 0);
});

2 Comments

I tried your code, but nothing changes, Still the saveandclosebutton always appears
@user2226785 can u make a fiddle .
0

Try Keyup. Change it to:

$('#visitID').keyup(function () {
        var len = $('#visitID').val().length;
        if ( len == 0)
            $("#saveandclosebutton").hide();
        else
            $("#saveandclosebutton").show();
    });

Comments

0

Try to use .is() with :empty selector as well as using keyup event to have live update when you're typing text inside your input:

$('#visitID').on('change keyup', function () {
    if ($(this).is(':empty')) {
        $("#saveandclosebutton").hide();
    } else {
        $("#saveandclosebutton").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.