How to react on input text events in jquery?

I’m trying to react on <input> events with jquery, to modify a background label in as soon as the input contains text:

$('#inpt').on('input', ':text', function() {
  Window.alert("test");
  $('#mytext').attr('background', 'red');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
  <input id="inpt" type="text" placeholder="Type in here" />
</div>

<span id="mytext" style="background:green">Test</span>

Result: nothing happens. I don’t even get the alert. Why?

10 thoughts on “How to react on input text events in jquery?”

Leave a Comment