ButtonSetCallback()

Attaches or changes the click callback on an existing button. Useful for dynamically updating button behavior.

Syntax
MaterialSB::ButtonSetCallback(Button, *Callback)
Parameters
Button The button element returned by Button().
*Callback Pointer to the procedure to call when clicked.
Return Value

None.

Remarks

This function adds a new click event listener. If you need to replace an existing callback, the old callback will still fire. For complete replacement, you would need to manage event listeners at the JavaScript level.

For input elements (text fields, checkboxes, etc.), use InputSetCallback() instead.

Example
Global myButton, clickCount = 0

Procedure FirstCallback()
  clickCount + 1
  MaterialSB::Toast("Click #" + Str(clickCount) + " - First callback")
EndProcedure

Procedure SecondCallback()
  clickCount + 1
  MaterialSB::Toast("Click #" + Str(clickCount) + " - Second callback added!")
EndProcedure

Procedure AddCallback()
  MaterialSB::ButtonSetCallback(myButton, @SecondCallback())
  MaterialSB::Toast("Second callback added to button")
EndProcedure

Procedure Main(Success)
  If Success
    MaterialSB::Row(MaterialSB::#Grid_Container)
      MaterialSB::Col(12)
        ; Create button without callback
        myButton = MaterialSB::Button("Click Me", #Null)
        
        ; Set initial callback
        MaterialSB::ButtonSetCallback(myButton, @FirstCallback())
        
        ; Button to add another callback
        MaterialSB::Button("Add Second Callback", @AddCallback(), MaterialSB::#Button_Outlined)
        
        MaterialSB::Append(MaterialSB::Paragraph("Click 'Add Second Callback' then click 'Click Me' to see both callbacks fire."))
        
      MaterialSB::CloseCurrentParent()
    MaterialSB::CloseCurrentParent()
  EndIf
EndProcedure

MaterialSB::Download(@Main())
See Also

Button(), InputSetCallback()