Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add description of relatedTarget to focusin/focusout event #2309

Closed
1 of 3 tasks
SetTrend opened this issue Apr 2, 2019 · 8 comments · Fixed by #9013
Closed
1 of 3 tasks

Add description of relatedTarget to focusin/focusout event #2309

SetTrend opened this issue Apr 2, 2019 · 8 comments · Fixed by #9013
Labels
Content:WebAPI Web API docs needs info Needs more information to review or act on.

Comments

@SetTrend
Copy link

SetTrend commented Apr 2, 2019

Request type

  • Please close this issue, I accidentally submitted it without adding any details
  • New documentation
  • Correction or update

Details

According to W3C documentation on UI events, the main feature of focusin and focusout events is that the relatedTarget property is set to the corresponding counter-element in the FocusEvent argument of these two events, whereas with focus and blur events, the relatedTarget property is always null.

Shouldn't this important feature be mentioned in the docs of the corresponding MDN help pages?

Many people wonder why the relatedTarget property is always null when they listen to focus/blur, not knowing there are focusin/focusout events available.

I'd like to suggest to add this information (and "see also" references between these four events) to the corresponding MDN pages.

Related Pages on MDN

https://developer.mozilla.org/en-US/docs/Web/API/Element/focusin_event
https://developer.mozilla.org/en-US/docs/Web/API/Element/focusout_event
https://developer.mozilla.org/en-US/docs/Web/API/Element/focus_event
https://developer.mozilla.org/en-US/docs/Web/API/Element/blur_event

@jmswisher
Copy link

@SetTrend Very helpful bug report! Thank you!

@chrisdavidmills chrisdavidmills transferred this issue from mdn/sprints Feb 12, 2021
@sideshowbarker sideshowbarker added the help wanted If you know something about this topic, we would love your help! label Jun 15, 2021
@himanshugarg
Copy link
Contributor

whereas with focus and blur events, the relatedTarget property is always null.

The following code sample shows that this is not true, at least not now:-

<!DOCTYPE html>
<html lang="en-US" charset="utf-8">
  <head>
  </head>
  <body>
    <form id="form">
      <input type="text" placeholder="text input">
      <input type="password" placeholder="password">
    </form>
  </body>
    <script type="text/javascript">
      const form = document.querySelector('input[type="password"]');
      
      form.addEventListener('focus', (event) => {
        event.target.style.background = 'pink';
        console.log('focus: ' + String(event.relatedTarget));
      });

      form.addEventListener('focusin', (event) => {
        event.target.style.background = '';
        console.log('focusin: ' + String(event.relatedTarget));
      });
      
      form.addEventListener('blur', (event) => {
        event.target.style.background = '';
        console.log('blur: ' + String(event.relatedTarget));
      });

      form.addEventListener('focusout', (event) => {
        event.target.style.background = '';
        console.log('focusout: ' + String(event.relatedTarget));
      });
    </script>
</html>
focus: [object HTMLInputElement] test.html:16:17
focusin: [object HTMLInputElement] test.html:21:17
blur: [object HTMLInputElement] test.html:26:17
focusout: [object HTMLInputElement] test.html:31:17

The only difference between those events as per the W3C documentation is in

  1. whether the event bubbles. This is noted in the MDN docs.
  2. the timing of event dispatch (before shifting in focusin/focusout, after in others). This is not noted in the MDN docs.

relatedTarget could be null in any of them in nested browsing contexts as that is a property of the FocusEvent interface.

What fixes, if any, should be made for this issue?

@sideshowbarker sideshowbarker added needs info Needs more information to review or act on. and removed help wanted If you know something about this topic, we would love your help! 30 minute task labels Sep 1, 2021
@sideshowbarker
Copy link
Collaborator

Closing for lack of response

@SetTrend
Copy link
Author

SetTrend commented Sep 7, 2021

Not so fast!

There are people out here who are working on business projects for their living, not having time to reply to you immediately just because you are having a slice of free spare time available.

You cannot come back to a ole thread after two years of idleness and expect to get a response immediately.

Please re-open.

@sideshowbarker sideshowbarker reopened this Sep 7, 2021
@himanshugarg
Copy link
Contributor

Apologies for this. Thanks for your time and feedback on this issue.

@sideshowbarker
Copy link
Collaborator

sideshowbarker commented Sep 8, 2021

@himanshugarg

Apologies for this.

Not your fault — mine for closing this issue prematurely.

Anyway, I’ll take this opportunity to tell you that I think the work you’ve been doing on issue triage and technical review of issues has been immensely helpful for the project — including the attention you’ve paid to getting some longstanding (and in some cases long-neglected…) issues the attention they’ve needed

@SetTrend
Copy link
Author

SetTrend commented Sep 8, 2021

So true.

I, on my part, will make sure to get back on this issue til end of the month.

@SetTrend
Copy link
Author

I now tested the new behaviour of blur and focus events, as @himanshugarg suggested. Thank you for bringing this issue back up again with your very valuable information, @himanshugarg!

The behaviour of these events in regards to relatedTarget indeed has changed over time. They now both return the counterpart element, if applicable.

focus and blur relatedTarget


So, it seems like there is no difference anymore between blur/focusout and focus/focusin, respective.

Considering thorough documentation on these events:

When is focusin supposed to be preferred over focus? When is focusout supposed to be preferred over blur? Or has one of these pairs become redundant now, since both mirror each other?

himanshugarg pushed a commit to himanshugarg/content that referenced this issue Sep 17, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Content:WebAPI Web API docs needs info Needs more information to review or act on.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants