How to track active widgets with dijit/focus
What is Dojo?
The Dojo is a JavaScript toolkit (library) that provides many utility modules and functions to create a web application. We can save time and speed up the development process using the predefined Dojo modules. Dojo includes language utilities, UI components, and more.
The dijit/focusmodule
Dijit is Dojo’s UI library. The dijit/focus module used to work with focused nodes and widgets. Using this module, we can get the focused node/widget and track the focus changes.
We can use the dijit/focus module to track active widgets by following the below steps:
Load the
dijit/focusmodule using therequirefunction.Use
watchmethod to track change onactiveStackarray, which contains the set of widgets that is focused.Listen for
widget-focusandwidget-blurto detect the widget focus and unfocus state.
Code example
Let's make a sample example to focus and unfocus a dialog widget using dijit/focus module:
Code explanation
In the above code snippet:
Line 4: We load the CSS file required for the Dijit widgets.
Line 8: We create a
inputelement with idnumber_input.Line 10: We load the Dojo library from the CDN server.
Lines 13–18: We load the
NumberTextBoxmodule present indijit/form. This is used to create a number field input box that only allows numeric values as input. We made the createdinputelement into a number text box.Line 20: We load the
dijit/focusmodule. Once that module is loaded, we assign the focused object to a variableDijitFocus.Lines 24–26: We add the event listener for
widget-focusevent. This event is fired when a widget is focused.Lines 29–31: We add the event listener for
widget-blurevent. This event is fired when a widget goes out of focus.Lines 34–37: We use the
watchfunction to track changes on theactiveStackarray that contains the ids of the focused widget elements.
Free Resources
- undefined by undefined