Home > Blockchain >  How to know if an input is clicked and unclicked in Javascript
How to know if an input is clicked and unclicked in Javascript

Time:06-02

I'm trying to figure out how to know when an input is clicked and when it is unclicked. Let me explain:

When ever you want to type something on an input field, you click on the input box and when you don't want to type, you click somewhere else and the input field is disabled.

<input type='text'>

Here as you can see, when you click on it, the field is enabled, and when you click somewhere else other than the field, it disables.

I just want to know when the field is disabled/unclicked.

CodePudding user response:

When you click on the input field focus event is fired. When you lose focus blur event is fired.

var elem = document.getElementById("fname")

elem.addEventListener("blur", myFunction);

function myFunction() {
  alert("Input field lost focus.");
}
<input type="text" id="fname">

CodePudding user response:

I believe you're just looking for onFocus.

<input type='text' onFocus={/* do something */} />

Read more about React events here.

  • Related