Attaching To Events
Statically in RML
The easiest way to attach to events with Lua is to write your code directly into the RML files, using the on*
attributes. When the event is fired three global variables are set up, document
, event
and element
.
element | The element that is currently being processed. |
document | The document the element that is currently being processed belongs to. |
event | The event that is currently being processed. |
Example:
<button onclick="print('Clicked!')"/>
Multiple statements can be called as in normal Lua.
Example:
<button onclick="print('Line 1') print('Line 2')"/>
Dynamically from Lua Code
The Lua version of AddEventListener
is modeled directly on Javascript. This allows you to bind any callable Lua function or string to an event.
Method 1:
function Init(document)
element = document:GetElementById('button')
element:AddEventListener('click', "print('Line 1') print('Line 2')", true)
end
Method 2:
function OnClick()
for i=1,10 do print('Line ' .. i) end
end
function Init(document)
element = document:GetElementById('button')
element:AddEventListener('click', OnClick, true)
end