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>