I'm trying to write some code that if the input is empty the input color changes to red. After user types in input, the red color is removed.
Html
<html>
<head>
<!-- add main style -->
<link rel="stylesheet" href="../css/style.css">
<!-- add JQuery -->
<script
src="https://code.jquery.com/jquery-3.3.1.js"
integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
crossorigin="anonymous"></script>
</head>
<body>
<div class="financeContainer">
<div class="searchBox">
<input id="financeSearch" type="text" class="financeInput" placeholder="Enter The Code">
<button id="financeBtn" class="financeSearchBt"><i class="fas fa-search financeSearchIcon"></i></button>
</div>
</div>
<script>
</script>
</body>
</html>
Css
.redColor{
color: red;
}
jQuery
$(document).ready(function () {
$("#financeSearch").keydown(function () {
if ($("#financeSearch").val().length>0){
$("#financeSearch").removeClass("redColor")
}
});
$(document).bind('keypress', function(e) {
if(e.keyCode==13){
$('#financeBtn').trigger('click');
}
});
$("#financeBtn").click(function () {
var financeKey = $("#financeSearch").val();
if (financeKey == ""){
$("#financeSearch").addClass("redColor")
}
});
When the button is submitted the input gets the class redColor.and I expect if the user types the red color is removed. But after typing 2 characters it's being done. I want it after the first character typed to be done.