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

290
Views
Create element from its constructor (without knowing the element tag)

Is it possible to create an element if you have its constructor but don't know its tag name?

Example:

class CustomElement extends HTMLElement {}
customElements.define(genRandomString(), CustomElement);

Is it possible to create a new instance of CustomElement? Or can you obtain its tag name in any way?

I'm asking this because I'm not sure what's the correct way to pass custom elements to functions that need them: do I need to pass around both the name and the constructor, in case one may need to either create an element (which requires the name) or to check whether an existing element is of this kind (which requires the constructor)?

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

0

Is this what you are asking for:

let foo = "bar";
window[ genRandomString() ] = foo;

And then you want to know what getRandomString produced?

Only when you monitor window can you capture what is added to it.

Same with WebComponents, only when you hook into customElements.define can you record what names are used.
In other words: mess with its prototype and potentially breaking its default execution.

But this only works from the moment you are in control; you could have missed all Web Components created already, at that time.

Can you explain WHY you want to do this?

about 4 years ago · Juan Pablo Isaza Report

0

Is it possible to create an element if you have its constructor but don't know its tag name?

If you have access to the constructor (like you said you have), you can just use

const instance = new CustomElement();

Please note that this might fail if the constructor doesn't conform to Custom Element constructor constraints.

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!