Button at runtime, click event not triggered

What could be the reasons why an onclick event isn't triggered?
I have two buttons. One was created at design time and the other at runtime.
In the browser (debug), I only see an event for the first one.

The code is very simple

constructor TMyListItem.Create( AHTMLIndex: Integer);
begin
  inherited Create;
  FHTMLIndex := AHTMLIndex;
  FButtonDel := TWebButton.Create( 'MyElementID' + FHTMLIndex.ToString);
  FButtonDel.ElementClassName := '';
  FButtonDel.OnClick := ButtonDelOnClick;
end;
procedure TMyListItem.ButtonDelOnClick( Sender: TObject);
begin
  console.log( Sender);
end;

I don't have a parent. But even with the form as the parent, it didn't work.

FButtonDel.Parent := AOwner; // AOwner = TheForm

Is it perhaps a timing problem?
Is there anything else that needs to be done when elements (with events) are created at runtime?

Is there a HTML element with ID = "MyElementIDx" in the HTML associated with the form?
As this is what your constructor call expects.
If you do not have it, create it with:

Button := TWebButton.Create(AOwner);
Button.Parent := ButtonParent;

The button HTML code is also generated at runtime and placed in the DOM.

The mistake was that I created the button first and then the HTML code in the DOM.

This works without an owner. I couldn't get it to work the other way around.
Not even with an owner and parent.

Thanks

I cannot see any issue here with runtime creation and assigning the event handler.
Test project is here:
Project1.zip (9.1 KB)

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.