Riot can listen for raw dom events on any element by adding the on* attribute to elements and referencing an event handler. Let's walk through a couple examples.
In the previous video, we auto-started our animation. Let's not auto-start the animation. Let's add some buttons that'll control the animation. There are life cycle events, which we just covered, but there are also raw DOM events, like onclick, onfocus, onchange.
These events can be handled by adding an attribute to an element. On this button, I'll listen for the onclick event. All Riot event attributes need to evaluate to a function reference to work properly. The reference, just like all other bindings, is always in the context of the tag.
I could write a new function down below, of this.click, but I don't need to include the "this" up here, in the binding. It's implied. What I want to do is actually trigger the start animation method on the trim tag. You can access any nested custom tag within the current tag by accessing this.tags.
Again, you don't write out "this." Then you can reference them by name. Trim. I want to reference...Not call, just reference...the startAnimation method. We'll set up stopAnimation in the same way. Now we can start and stop the animation.
Let's add an input so we can change the animation text. We'll bind its value to what, and on key up, we'll call the what change handler. We'll add a what change handler to this tag.
It'll receive an event object that has been normalized to work consistently across all browsers. We can access the event's target, which will be the input, and get its value and update this.what with it. Now we can update the trim value on the fly.
A nice little feature that Riot also has is that you can add a name attribute to any element to get a quick reference to that element. If I give this input a name of whatInput, then I can easily reference that element with self.whatInput as an alternative to e.target or in other scenarios.
Remember that the attribute-based event handlers from this video only work with raw DOM events. I've included a list of those events at the bottom of the code example.