C
C
Counter2018-03-03 15:19:18
JavaScript
Counter, 2018-03-03 15:19:18

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

3 answer(s)
V
Vladimir, 2018-03-03
@kalatushki

poke

C
coderisimo, 2018-03-03
@coderisimo

focus ?
https://codepen.io/coderisimo/pen/JpVOGo

C
Counter, 2018-03-03
@Counter

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);
  })
}

Here is a solution that solves the issue 100%. Colleagues from stackoverflow suggested.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question