Remove Style on Element
Remove inline styles from an HTML element swiftly and cleanly using pure JavaScript:
Removing specific inline styles
If you want to eliminate specific styles and leave the rest intact, no worries; we've got you covered:
Now, the width
and height
styles have been given a one-way ticket out of your code.
Beating stubborn or external styles
Sometimes, styles are just as stubborn as mules, or you have to tackle external styles. We'll ambush them using regex:
Here, we craftily match each persistent style and drop them like hot potatoes.
Resetting the default styles
Sometimes, you might just want to restore the default value of a style instead of removing it totally:
The omnipotent loop
Looping is not just for breakfast cereals! When you are dealing with multiple elements, the loop becomes your best ally:
Reapplying styles: Handle with care
After using removeAttribute("style")
, remember to rehang the styles you want:
Event handling: Timing is everything
Selecting the ideal event for attaching the style removal action will greatly enhance your solution. Here's a way to attach an event listener to your element:
Was this article helpful?