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.

6
  • have you tried using the 'onChange' event. w3schools.com/jsref/tryit.asp?filename=tryjsref_onchange
    – Skywalker
    Commented Oct 20, 2015 at 13:01
  • @Onilol I've seen that question but I don't know how to use $("..").is(":focus")
    – Philies
    Commented Oct 20, 2015 at 13:09
  • I've tried this: if(document.getElementById(array[0]).is(":focus"))
    – Philies
    Commented Oct 20, 2015 at 13:11
  • @Philies that checks if an element has the pseudo-class focus . Try something like this : $(my_element_id_here).is(":focus")
    – Onilol
    Commented Oct 20, 2015 at 13:13
  • @Onilol: OP does not use jQuery, but pure Javascript ;)
    – fboes
    Commented Oct 20, 2015 at 13:23

1 Answer 1

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();" >

1
  • Idea: use onfocus="setMeter(this);" and function setMeter(el) to access properties of clicked element as well
    – fboes
    Commented Oct 20, 2015 at 13:18

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.