3

I want to trigger one handler when an input field is clicked, and another handler when it is deselected (i.e. if someone clicks outside of the field). Is there a way to accomplish this?

The click handler is simple enough:

<input onClick={props.clicked}>

Is it possible to create an "unclick" handler?

Ran Marciano
  • 1,431
  • 5
  • 13
  • 30
James Shapiro
  • 4,805
  • 3
  • 31
  • 46
  • Asked & answered before, and if you do a web search for "react outside click handler" you can find *many* results and solutions. – Drew Reese Nov 13 '20 at 07:27
  • @DrewReese I know I included the "React" tag, but I think the question may still be useful for people looking for a solution outside of React. The "onBlur" answer looks to have solved my problem. – James Shapiro Nov 13 '20 at 07:29
  • Are you asking for a non-react-specific outside click handler solution then? – Drew Reese Nov 13 '20 at 07:34
  • @DrewReese When I asked I was just looking for something that would solve my problem. Now that an answer has been provided that is not React-specific, I think it would make sense to make the question not specific to React either, so that people not working in React can find it. – James Shapiro Nov 13 '20 at 07:41

1 Answers1

3

I think onBlur event should be suitable for this

Ran Marciano
  • 1,431
  • 5
  • 13
  • 30