0

I have input. When i focus out on that input the focus out event gets triggered. But under that input i am having

<div id="myDiv">
  <ul>
     <li>1</li>
  </ul>
</div>

i want focus out event to be triggered when the inputs gets focus out - but when this under - when it is clicked - i don't want the focusing out of the input to ba happened.

I tried this here:

How to exclude Id from focusout

$('#id').focusout (function (e) {

    if (e.relatedTarget && e.relatedTarget.id === 'dontFocusOut') {
        return;
    }
    //do your thing

});

but it does not work for me - in event.relatedTarget i always get null.

peco123
  • 91
  • 1
  • 9
  • That is a weird requirement. What if the user clicks on `myDiv` and *then* 10 seconds later clicks on another element... You will not have the `focusout` event triggered. Please provide a snippet that includes the `input` element, preferably a runnable snippet (use the toolbar), so we can reproduce your issue. – trincot Aug 04 '21 at 10:28
  • can you also explain why you don't want the focus out - perhaps there is a better way to achieve your desired result rather than removing the focus out – Pete Aug 04 '21 at 10:32
  • @Pete i am builing autocomplete component. So clicking on the dropdown - dropdown options are opened. The user can choose some of the options below. If he choose some option. then focusOut is triggered automatically and i can't listen to other changes where i am picking up the chosen autocomplete option – peco123 Aug 04 '21 at 10:35

1 Answers1

-1

You can do like this:

function my_fun(){
  alert('Focus Out');
}
<input type="text" placeHolder="After focusout it will work" onfocusout="my_fun();" />
Hedayat H
  • 329
  • 1
  • 7