-3

How is it possible to and what is best practice for listening and handling an event using Javascript which shall be triggered by pressing backspace on an <input type="text" /> which also is triggered when the inputs value is empty and was empty before the event?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
loresIpsos
  • 327
  • 1
  • 2
  • 12

1 Answers1

0

Do you like this way? Only triggers if you press backspace inside the input... take a look!(Using Jquery)

$(document).ready(function(){
    $(document).on('keydown','input[type="text"]',function(e){

        if(event.keyCode == 8){
            alert('backspace');
        }

        });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text">

And this code triggers if the input is empty

    if($('#input').val() == ''){
        alert('input empty');
    }
mindmaster
  • 1,828
  • 12
  • 22