Skip to content

Instantly share code, notes, and snippets.

@yairEO
Last active May 6, 2024 12:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yairEO/94280c85050274881813bbfe27c6a121 to your computer and use it in GitHub Desktop.
Save yairEO/94280c85050274881813bbfe27c6a121 to your computer and use it in GitHub Desktop.
Remove a single property from DOM element "style" attribute
const removeStyleProp = (elm, prop) =>
elm.style.cssText = elm.style.cssText // cssText automatically (luckily) adds spaces between declarations
.split('; ')
.filter(p => !p.startsWith(prop) )
.join(';');
@yairEO
Copy link
Author

yairEO commented Dec 22, 2021

Usage: (demo)

removeStyleProp(document.body, 'right')

@yairEO
Copy link
Author

yairEO commented Dec 22, 2021

multi-props removal:

const removeStyleProp = (elm, props) => 
  elm.style.cssText = elm.style.cssText.split('; ') 
    .filter(sp => props.every(p => !sp.startsWith(p)) ) 
    .join(';');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment