Skip to content
Advertisement

WebComponent that extends HTMLButtonElement is not calling constructor() and connectedCallBack()

I’m trying to create a web component button but when I add it in HTML the constructor() function is never being called.

JavaScript
JavaScript
JavaScript

I’m facing some problems:

1- The constructor() is not being called so my event listener is not being added to the element;
2- connectedCallback () lifecycle is not being called too, so my button is not getting the title passed as a parameter;

Why this is happening and how I can fix this?

constructor() is only being called if i instantiate my custom element and append in `body:

JavaScript

But I would use my custom selector “my-button” in HTML instead of appending it.

Advertisement

Answer

Re: from comments

That link is to the HTMLButtonElement That is a standard element supported by all Browsers.

There are 2 different flavored Web Components:
details see Web Components : extending native elements

  • Autonomous Elements (extend from HTMLElement)
  • Customized Built-In Elements (extend from any Element)

But Apple/WebKit will not implement the latter as stated in 2016:

https://github.com/WICG/webcomponents/issues/509

Advertisement