Answer the question
In order to leave comments, you need to log in
Keeping track of the value in the input while it is in focus?
There are n-th number of inputs. They are all with the same class. Each time the page is loaded, their number varies from 10 to 50. You need to track the focus and the values ​​of the inputa on which the focus is. The example below doesn't work
<input class="myinput">
<input class="myinput">
<input class="myinput">
<input class="myinput">
<input class="myinput">
<input class="myinput">
<input class="myinput">
var elem = document.getElementsByClassName('myinput');
for (var i = 0; i < elem.length; i++) {
elem[i].id='phonemid' +i;
};
setInterval(function() {
for (var i = 0; i < elem.length; i++) {
var last = document.getElementById("phonemid" +i);
last.onfocus = function() {
var my = elem[i];
console.log(my.value);};
};
}, 300);
Answer the question
In order to leave comments, you need to log in
var elem = document.getElementsByClassName('myinput');
for (let i = 0; i < elem.length; i++) {
elem[i].addEventListener('input', function() {
console.log(this.value, 'this index ' + i);
})
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question