Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

177
Views
JavaScript - Create elements using web component class

I have a web component that only works when some properties are set, when I create the component with Document.createElement() I can't pass properties then an error happens in my component.

Is it possible to create the component by creating an instance of the component class or something similar and be able to pass parameters to it's constructor?

customElements.define('x-card', class extends HTMLDivElement {
//...
}, { extends: 'div' });

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can create a new instance using new WCard. This requires having access to the class though.

Please note that custom element names preferrably should not start with x-.

So change your code:

export class WCard extends HTMLDivElement {
//...
}

customElements.define('w-card', WCard, { extends: 'div' });

window.WCard = WCard;

This allows you to call new on the class globally, or if your code imports the class.

const wcard = new WCard(...args);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!