2

When I go back and erase the data leaving the input field blank, the background does not go back to it's original color UNLESS I TYPE A "0".

I need the background color of the DIV to revert back to its original color when blank.

What am I doing wrong?

<script>
$(document).ready(function() {

    $("#id").keypress(function() {
        if ($("#id").val().length > 0) $("#in").css("background-color", "red");
        else {
            if ($("#id").val().length == 0) $("#in").css("background-color", "grey");
        }
    });

});​
</script>
1
  • 1
    Why are you testing the .length a second time? That's what the else is for. Commented Apr 9, 2012 at 13:57

5 Answers 5

3

The problem is because you are using keypress which is called before the action of the key is executed. If you used keyup this will work:

$("#id").keyup(function() {
    if ($(this).val().length > 0) {
        $("#in").css("background-color", "red");
    }
    else {
        $("#in").css("background-color", "grey");
    }
});

Also, as @mblase75 pointed out, you do not need to test the length of the value in the else condition.

Example fiddle

If you wanted to simplify this code even further, you can simply use a ternary statement with .val().length as the condition, as a positive integer will equate to true:

$("#id").keyup(function() {
    $(this).css("background-color", $(this).val().length ? "red" : "grey");
});
Sign up to request clarification or add additional context in comments.

Comments

2

You would want to use keyup to account for the character that was just entered:

$(function(){
    $("#id").keyup(function() {
        $("#in").css("background-color", $(this).val().length > 0 ? "red" : "grey");
     });
 });​

Here's a jsFiddle to demonstrate.

1 Comment

You've used this for both #in and #id.
0

This one should work...

<script>

$(document).ready(function() {

    $("#id").keyup(function() {
        if($("#id").val().length > 0) {
           $("#in").css("background-color", "red");
        }
        else {
           $("#in").css("background-color", "grey");
        }
    });
});​

</script>

1 Comment

I don't think keypress will work for what OP is trying to do.
0

In your case, 'keypress' event has called before input.val().length changed. And your code run selector retreiving five times, insted one.

Look to this code, it works fine:

html:

<input id="id" type="text" autocomplete="off">
<div class="in"></div>​​​

css:

.in {width:200px; height:20px; background-color:#grey;}
.in-red {background-color:red;}

​ js:

$(function(){
    var $in = $('.in');
    $('#id').on('keyup', function(){
        $this = $(this);
        if ($this.val().length > 0) {
            $in.addClass('in-red');
        } else {
            $in.removeClass('in-red');
        }
    });
});​

And you can test it on http://jsfiddle.net/Qhkev/1/

Comments

-2

Use keyup

<script>
$(document).ready(function(){

$("#id").keyup(function() {
    if($("#id").val().length > 0) $("#in").css("background-color", "red");
  else {
    if($("#id").val().length == 0) $("#in").css("background-color", "grey");
 }

 });
 });
 </script>

1 Comment

If you vote down please comment why the vote down. I tested this answer before posting and it works.

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.