Skip to Content

focusOnElement()

Focus on an element for tab navigability or otherwise.

JavaScript September 25, 2019

Usage

JavaScript
nebula.focusOnElement(element)

Parameters

element
(Required) (Object) The element to focus on
Default: None

Parameter Notes

If the element desired to be focused on is not typically focusable, this function makes it temporarily focusable.

Request or provide clarification »

Examples

Scroll to an element and then set tab navigability to start there as well.

JavaScript
jQuery('html, body').animate({
    scrollTop: Math.floor(jQuery(scrollElement).offset().top-offset+pOffset)
}, scrollSpeed, function(){
    nebula.focusOnElement(scrollElement); //Start tab navigability here too
});

Source File

Located in /assets/js/nebula.js on line 3832.

JavaScript
nebula.focusOnElement = function(element){
    if ( !element.length ){
        return;
    }

    //If the element is not focusable itself, add tabindex to make focusable and remove again
    if ( !element.is(':focusable') ){ //Uses custom expression defined at the bottom of this file
        element.attr('tabindex', -1).on('blur focusout', function(){
            jQuery(this).removeAttr('tabindex');
        });
    }

    element.trigger('focus'); //Focus on the element
};

Override

To override or disable this JavaScript function, simply redeclare it with the exact same function name.

JavaScript
nebula.focusOnElement = function(element){
    //Write your own code here, leave it blank, or return false.
}