Work with class identifiers

Class identifiers are a very important part of the components, since the whole appearance is built on their basis.

There are several methods for interacting with them. The names of the methods speak for themselves.

bool hasClass(const string& className) const;
Component* removeClass(const string& className);
Component* addClass(const string& className);
Component* toggleClass(const string& className);

As an example, when interactions with class identifiers may be needed, this is the creation of a component for Checkbox in which the change of picture may occur due to the change of the class identifier from unchecked to checked and vice versa.

Getting components by class identifier

As already described earlier, an identifier is used to access a component. But the component can be obtained by the class identifier, but with some caveats.

Since several components can have the same class identifier, a collection of components with the given class identifier will be returned.

To get the collection, use the getElementsByClassName method:

Components getElementsByClassName(const string& className);

The Components class is a wrapper over a collection of components. The following actions can be performed on the collection:

  1. Set each element of the collection to the same listener using the addEventListener method, which completely repeats the method of the Component class;

  2. Call for each collection item a callback function with the first parameter passing a pointer to the current collection item using the each method

    void each(function< void(Component* sender)> callback);
  3. Get the component by number using the at method or the operator[]:

    Component* at(size_t index);
    Component* operator[](size_t index);

  4. Get the size of the collection using the size method:

    size_t size();

Last updated