0

I have an input field which will be updated every x seconds:

<input type="number" name="METER" id="METER.NUM" min="0" max="500" step="0.10" oninput="setMeter(currentValue);" >

Now, I want to check if the input field has focus (user clicked into the field).

If the field has focus:

  • I will stop updating the field and the user can put some value into the field
  • Then the value should be passed to a function

This is the javascript code:

if (document.getElementById(array[0]).name == "METER") {
    // check if the input field has focus
    // stop updating
}

What's wrong with my approch/code? I think one problem is, that the function call by oninput doesn't work. But it works by onClick.

fboes
  • 2,149
  • 16
  • 17
Philies
  • 233
  • 1
  • 4
  • 15

1 Answers1

1

use onfocus = myFunction()

function setMeter() {
    console.log('hey, im focused');
    // do stuff here
}
<input type="number" name="METER" id="METER.NUM" min="0" max="500" step="0.10" onfocus="setMeter();" >
indubitablee
  • 8,136
  • 2
  • 25
  • 49
  • Idea: use `onfocus="setMeter(this);"` and `function setMeter(el)` to access properties of clicked element as well – fboes Oct 20 '15 at 13:18