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?
Asked
Active
Viewed 33 times
1 Answers
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
-
huh.. so it was the $(this) keyword I needed. Thanks so much, easily fixed my code! – misner3456 Jul 24 '19 at 02:19
-
@misner3456 do you know why using `$(this)` solves your problem tho? – yqlim Jul 24 '19 at 02:21
-
@YongQuan yeah I think I sort of get it. $(this) responds to the specific element clicked on – misner3456 Jul 24 '19 at 02:31