How to check if any form element inside a fieldset has focus

connexo

I want to partially validate a form by validating all elements inside a fieldset.

As a trigger for that validation, I'm listening to focusout events which bubble up to each fieldset.

I want to perform the validation if no form element inside that fieldset has :focus.

I've attempted to do like this:

const fieldsets = [...document.querySelectorAll('fieldset')]

for (const fieldset of fieldsets) {
  fieldset.addEventListener('focusout', (e) => {
    let focussedElements = e.currentTarget.querySelectorAll(':focus');
    if (focussedElements.length === 0) {
      console.log(`no element inside fieldset #${e.currentTarget.id} has focus`)
    } else if (focussedElements.length > 0) {
      console.log(`element ${focussedElements[0]} inside fieldset #${e.currentTarget.id} has focus`)
    }
  })
}
<fieldset id="fs1">
  <legend>Fieldset 1</legend>
  <input type="text">
  <input type="text">
</fieldset>

<fieldset id="fs2">
  <legend>Fieldset 2</legend>
  <input type="text">
  <select>
    <option>1</option>
    <option>2</option>
  </select>
</fieldset>

but e.currentTarget.querySelectorAll(':focus') always has lengthof 0.

How do I reliably check if a fieldset has a child element that has :focus?

Note: I don't want to use jQuery or any other library for the task.

Alohci

Don't you just need to wait for the focus on the new element to happen? Like this:

const fieldsets = [...document.querySelectorAll('fieldset')]

for (const fieldset of fieldsets) {
  fieldset.addEventListener('focusout', (e) => {
    let ct = e.currentTarget;
    setTimeout(() => {
      let focussedElements = ct.querySelectorAll(':focus');
      if (focussedElements.length === 0) {
        console.log(`no element inside fieldset #${ct.id} has focus`)
      } else if (focussedElements.length > 0) {
        console.log(`element ${focussedElements[0]} inside fieldset #${ct.id} has focus`)
      }
    }, 10)
  })
}
<fieldset id="fs1">
  <legend>Fieldset 1</legend>
  <input type="text">
  <input type="text">
</fieldset>

<fieldset id="fs2">
  <legend>Fieldset 2</legend>
  <input type="text">
  <select>
    <option>1</option>
    <option>2</option>
  </select>
</fieldset>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to check if any span element in inside div has empty value

How to check the input element is inside form element or not

Check if Form has focus or is active

Codeception: Check if element has focus?

How to check if element has any children in Javascript?

How to check if an element has 'any' value Cypress?

How to set focus on first form element in any bootstrap modal

How do I check if a form element (input or button, etc) is disabled by itself or a parent fieldset in Javascript?

How to check if button has focus?

how to check that lineedit has focus?

Check if element in array of elements has focus

Check any element of an array inside a list

How to trigger focus event on element inside controller?

Change event to check change on any form element

hide bootstrap popover on blur EXCEPT if element inside popover has focus

How to focus next element on ngFor in a form angular?

how to check if element has this value or not

there is a correct way to put a "title" for a fieldset inside a form?

check if button has focus

Check if input has focus

How can I get Index of any element in python numpy array that has list inside?

How to check if parent element has a child element

Vuejs - Focus element check

How to use map to check if each list element is inside any set of ranges? (TypeError: len() of unsized object)

How to remove fieldset border only but not the border of elements inside the fieldset?

In case of a Fieldset that is nested inside a Fieldset - how should the Legends be accessible to screenreaders?

Check if this array includes any name that has "John" inside of it

Check if IEnumerable<object> has any values inside without getting IndexOutOfRangeException

Is there any posibility to check if function has inside call of another function?