What could be the reason why my focus and blur event listener is not being activated?

It seems like everything is in order here, but for some reason, the event just won't fire...

const element = (this.agGridElm.nativeElement as HTMLElement);
element.addEventListener('focus', (focusEvent: FocusEvent) => {
  element.classList.add('focused');
}, { capture: true, passive: true });

element.addEventListener('blur', (focusEvent: FocusEvent) => {
  element.classList.remove('focused');
}, { capture: true, passive: true });

I've tried with and without the third argument { capture: true, passive: true }, but nothing changes... I find it strange that both focus and blur events are registered when inspected using Chrome debugger's Event Listeners, yet they do not trigger upon clicking on the element.

Interestingly, I had success implementing a similar functionality with the mouseleave event. So, why aren't focus and blur cooperating? The code snippet below functioned properly...

element.addEventListener('mouseleave', (mouseEvent: MouseEvent) => {
  console.log('Hello!');
});

Answer №1

It turns out that in order for the event listeners focus, blur, focusin, and focusout to work on a div element, a tabindex is required. The solution I found was to include the following snippet within the element I want to observe.

<div tabindex="-1"></div>

Implementing the tabindex fixed all issues with my code!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Interacting with Django backend via AJAX to trigger Python functions using jQuery/AJAX

Currently, I am developing a small website using Django. Within this project, there are DateTime fields that need to be handled. My intention is to utilize ajax by implementing another button to establish the UTC date while taking into consideration sola ...

How to use Javascript, HTML5, and AngularJS to display and print a PDF document within a

I have a situation where I need to load a Base64 encoded PDF as a String from my server into JavaScript in my client application which is built using AngularJS and HTML5. The structure of my HTML code is as follows: <div id="printablePdfContainer"> ...

The custom server was functioning properly, but as soon as I altered the file directory, an error occurred on Next.js

After creating "create-next-app," I successfully set up the folder structure as follows: +client2 --.next --node_modules --public --... --server.js However, when I move my "server.js" file to a different location: +client2 --.next --no ...

Creating a unique design with text in a circular format

When styling customer initials within a circle, I sometimes encounter issues with font alignment, as seen in the image below where EW is not centered properly. In this code snippet, I am using border-radius to create circular shapes for displaying custome ...

How to set the element in the render method in Backbone?

Currently, I am in the process of developing a web page utilizing BackboneJS. The structure of the HTML page consists of two divs acting as columns where each item is supposed to be displayed in either the first or second column. However, I am facing an is ...

When working with TypeScript, how do you determine the appropriate usage between "let" and "const"?

For TypeScript, under what circumstances would you choose to use "let" versus "const"? ...

How to Modify a File Inside a Compressed File Using JSZip

Is it possible to modify a file within a zipped file using JSZip? I have searched for answers and reviewed the API, but I am unable to find a solution. Any assistance on this matter would be highly appreciated. Thank you in advance! ...

Is it necessary to compile the React JavaScript project before uploading it to npm?

Hey there, I'm currently in the process of publishing a React JavaScript component on npm. I have a question regarding whether or not I need to build the project and deploy the build folder. Your input would be greatly appreciated. Thanks! ...

Extracting data from datepicker and passing it to PHP

My appointment booking form consists of 1 input and 2 selects: First: an input field using jQuery for selecting a date. Second: a select dropdown for choosing a hairdressing option. Third: another select dropdown that displays available times based on the ...

An object resulting from the combination of two separate objects

After reading a helpful solution on StackOverflow about merging properties of JavaScript objects dynamically, I learned how to utilize the spread operator in Typescript. However, one question still remains unanswered - what will be the type of the object c ...

What is the best way to trigger a function after the v-model has been updated

When attempting to filter an array of objects in Vue using input, I encountered issues with Salvattore not functioning correctly for building a grid of the filtered elements. It seems that calling the rescanMediaQueries() function after changes to my v-mod ...

Use JavaScript to limit Google Analytics and Yandex.Metrica to track only the referral sources and screen sizes

I prefer not to include external JavaScript on my website for unnecessary tracking purposes. However, I do need to gather referrer and screen size information, which cannot be achieved with a regular HTML img tag alone. What is the most standard-complian ...

Having trouble getting Three.js JSON models to cast shadows properly?

Recently, I've been experimenting with blender exported models in Three.js. I have successfully imported a model and observed how light interacts with the object. While a directionalLight is illuminating the front-facing parts, I'm facing an issu ...

How to incorporate "selectAllow" when dealing with dates in FullCalendar

I've been attempting to restrict user selection between two specific dates, but so far I haven't been able to make it work. The only way I have managed to do it is by following the routine specified in the businessHours documentation. Is there a ...

Adjusting the Camera in three.js

I'm relatively new to three.js and webgl programming. I managed to create a box in three.js, which is functioning correctly. However, I encountered an issue where the box disappears when I try setting the camera position along the z-axis (e.g., camera ...

What causes the return value of keyof to vary in this particular case?

type AppleNode = { type: 'Apple' name: string score: number } type BananaNode = { type: 'Banana' id: number score: number } type FruitNodes = AppleNode | BananaNode type fruitTest = { [P in keyof FruitNodes]: 21 } // Th ...

Implementing THREE.js (along with THREEx and AR.js) CDN into my React component: A step-by-step guide

I found that using React for projects like this can be less cumbersome than Angular, and you can still incorporate "plain" JavaScript into your project. In my react project's index.html file, I have added the following script tags in order to integrat ...

Obtaining a variable from within two nested functions

I'm looking for a solution where I can retrieve a variable from inside a function using an ajax call. For example: function returnStuff(){ $.ajax({ //do something }).done(function(response){ return response; }) return response; } The ...

What is the best way to organize code within the main.ts file in a Vue 3 project?

New to Typescript and vue, I am eager to figure out how I can extract this code from my main.ts file. I'm concerned about it becoming messy as more icons are added. const app = createApp(App); /* import the fontawesome core */ import { library } from ...

Sync your data effortlessly with jQuery Datalink, the ultimate solution for

While experimenting with the jQuery Data linking proposal by Microsoft, I came across an unexpected discovery. There seems to be an additional property present in my objects, and I am curious as to why it is there. Initially, I thought it was a mistake on ...