Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
inert-unert.js: a quick utility for applying or removing the inert property
// inert-unert.js: a quick utility for applying or removing the inert property
// - @scottjehl, @filamentgroup
// (note: inert support polyfill is still needed in some browsers)
// usage:
// when a modal element such as a dialog is active,
// this function will make unrelated elements inert, aiming to affect as few as possible
// example: inert( document.querySelector(".modal-open") );
function inert( allButThisElement ){
function inertSiblings( node ){
if( node.parentNode ){
node.parentNode.childNodes.forEach(function(elem){
if( elem !== node && elem.nodeType === 1 ){
elem.inert = true;
}
});
if( node.parentNode !== document.body ){
inertSiblings(node.parentNode);
}
}
}
inertSiblings( allButThisElement );
}
// remove the inerts, when elements should be reachable again
// example: unert();
function unert(){
document.querySelectorAll( "[inert]" ).forEach(function(elem){
elem.inert = false;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment