I'm trying to change input value and display an alert based on value written by the user. Now it doesn't work unless it goes out of focus. Can I make it work immediately without any waiting period?
jQuery('input').on('change', function () {
var myInput = jQuery(this);
if (myInput.val() < 0.2 ) {
myInput.val('0.2');
jQuery('.alert').css('display', 'block')
}
else {
jQuery('.alert').css('display', 'none')
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input value="">
<p class="alert" style="display: none">minimum value is 0.2</p>
keyupinstead ofchange. The change event fires when you loose focus of the input