Answer the question
In order to leave comments, you need to log in
How to hang focus action on child elements in js?
Hello
, I would like to add a class after the focus is removed from the form, but the focus of the parent does not extend to the child elements, and therefore, after the user enters the input text and clicks on the free part of the site, nothing happens, I would like the focus to work parent and on child elements, i used focusout, blur but nothing worked, does anyone know?
Here is the form
<form action="#">
<div class="form_block" tabindex="0">
<input type="text" id="fred">
<div class="text-arr"></div>
</div>
</form>
$('form_block').on("focusout", ()=>{
$('form_block').addClass('dd')
})
Answer the question
In order to leave comments, you need to log in
const form = document.querySelector('form');
for (const element of form.elements) {
const wrapper = element.closest('.form_block');
if (wrapper !== null) {
element.addEventListener('blur', (event) => {
if (!wrapper.contains(event.relatedTarget)) {
wrapper.classList.add('dd');
}
});
}
}
tabindex="0"
you can remove.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question