I want to add or remove a CSS class from an element, based on a boolean. I want a nicer version of this:
if (condition) {
element.classes.add('important');
} else {
element.classes.remove('important');
}
Dart's HTML library has two ways to toggle CSS classes on an element. (Well, four ways if you count toggleAll :)
To add a class if it's missing, or remove a class if it already exists, use toggle(String className)
:
element.classes.toggle('important');
To toggle a CSS class based on a boolean value, use toggle(String className, [bool shouldAdd])
:
element.classes.toggle('important', condition);
Here's the original feature request, which links to the commit that adds this feature: https://code.google.com/p/dart/issues/detail?id=11741
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With