2

I am using React, and have the following setup:

<input onBlur={(e) => {this.removeEditable(e)}} //...etc
/>

and

removeEditable(e) {    
//Do Something
  }

My problem is that I am trying to get the element clicked on from the event fired onBlur. Please note, this may not be the same element as the element that originated the click. For example, the input box above may be active, but the onblur event will get fired when the user clicks outside of the input box.

I have tried event.target but that gives the input element defined above - not the element that the actual click happened on.

Can this be done?

I am pretty sure for drag and drop this can be done but not sure about blur?

I have tried event.currentTarget as was previously suggested below (and then removed), but that also returns the input element, in the same was as event.target does.

As a note, the element that is being clicked on is position absolute, and overlaid on top of a div without position type defined (eg standard position) - I am not sure if this could be affecting the event?

I have logged the event.currentTarget and it seems to always return the input outlined above.

Alex
  • 3,730
  • 9
  • 43
  • 94
  • The `onblur` event actually only appears on the input element, not on the element you clicked on. – Christoph Jun 16 '17 at 14:27
  • `I am trying to get the element clicked on from the event fired onBlur` - See answers in top result from google search on `get the element clicked on from the event fired onBlur` ► [**When onblur occurs, how can I find out which element focus went *to*?**](https://stackoverflow.com/questions/121499/when-onblur-occurs-how-can-i-find-out-which-element-focus-went-to) Hope it helps. – Nope Jun 16 '17 at 14:28
  • @Christoph I know that, but onBlur is only triggered when the input element is in focus – Alex Jun 16 '17 at 14:28
  • So I don't get the question to be honest. You cannot get the element you clicked on from a `blur` event, you need the `click` event for this. Also, what happens if the user does not click, but tabs to the next element - then you have no click event to begin with. You can try `document.activeElement` to see if another element got the focus though. – Christoph Jun 16 '17 at 14:30
  • @Fran fair point - I could have googled that better – Alex Jun 16 '17 at 14:35
  • 1
    @Christoph Wouldn't the `focus` event trigger wether the user clicks on the input or tabs to it? – Baruch Jun 16 '17 at 15:01
  • 1
    @Baruch This depends. For an `input` element, you are correct. But not every element emits a `focus` event and a click could be on a non-focus-able element, while tab automatically jumps to the next element that actually is able to receive focus. – Christoph Jun 16 '17 at 15:10

0 Answers0