Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I toggle a CSS class based on a boolean, with Dart?

Tags:

dart

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');
}
like image 710
Seth Ladd Avatar asked Jul 19 '13 21:07

Seth Ladd


1 Answers

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

like image 90
Seth Ladd Avatar answered Oct 20 '22 02:10

Seth Ladd