0

I'm creating a table that'll make extensive use of <input type="number"> fields and I want to make the background color of the table turn red if a number isn't placed inside.

Here's example html for my table:

<tr>
  <td>
      <input type="number" class="amount" name="amount">
  </td>
</tr>

Here's the jquery code I'm using to validate the input:

$(".amount").blur(function() {
   if ($(this).val() ==="") {
      $(this).parents("td").css("background-color", "#CDC9C9");
    }
});

This works fine if an incorrect value is entered the first time, but if you change the input until it's correct the background-color doesn't change.

I'd think jquery would evaluate the blur method each time something is changed but that's not the case. Why?

Also, my tablerows have alternating background colors, so I'd prefer not to get into something too complicated with setting the background color as that might backfire.

Code snippet here:

$(".amount").blur(function() {
  if ($(this).val() === "") {
    $(this).parents("td").css("background-color", "#CDC9C9");
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
  <td>
    <input type="number" class="amount" name="amount">
  </td>
</tr>
</table>

3
  • you can use input event instead of blur.. how about that? Commented Apr 9, 2015 at 5:29
  • @BrijeshBhatt -which one would you recommend? Commented Apr 9, 2015 at 5:31
  • i will recomment input event.. Commented Apr 9, 2015 at 5:32

2 Answers 2

1

The change event fires anytime an input has changed and is blurred. The input event fires with every keystroke. You can use either to add and remove a class that gives the correct background color like this:

$(".amount").on("change", function() {
    if ($(this).val() === "") {
        $(this).closest("td").addClass("red-class");
    } else {
        $(this).closest("td").removeClass("red-class");
    }
});

And add some CSS:

<style>
    .red-class {
        background-color: red;
    }
</style>
Sign up to request clarification or add additional context in comments.

2 Comments

I went ahead and used the addClass/removeClass method with blur and it worked just fine.
That's good.. I've found blur can behave differently depending on the device and change gives basically the same effect but more a bit more consistently. Also change does nothing if the actual value of the input didn't change when blurred - which is a bit less expensive.
0

Try key events such as keyup or keypress or keydown on input fields. Call that blur function of yours inside this event. Example :

$(".amount").keyup(function() {
  if ($(this).val() == "") {
    $(this).parents("td").addClass("redClass");
  } else {
    $(this).parents("td").addClass("greenClass");
  }
});
.redClass {
  background-color: red;
}

.greenClass {
  background-color: green;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr>
    <td>
      <input type="number" class="amount" name="amount">
    </td>
  </tr>
</table>

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.