1

So my goal is to replace text in input fields using my javascript/jquery code. I want to target each input field by incremented ID using a For Loop, but my code is not working?

misner3456
  • 404
  • 2
  • 13

1 Answers1

1

for(i = 1; i < 3; i++) {
    $("#inp"+i).on('change keyup paste input',function(){
        var a=$(this).val();
        var b=a
        .replace(/hey/g,'hello');
        $(this).val(b);
    });
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="inp1">
<input type="text" id="inp2">
BeiBei ZHU
  • 343
  • 2
  • 12