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

181
Views
Using functions defined within web component

I have build my web component DataUint and assigned it a tag <data-uint> like this:

class DataUint extends HTMLElement
{
   ...
   set value(x) { ... }
   ...
}

customElements.define("data-uint", DataUint);

When creating and accessing a component like this:

x = document.createElement('data-uint');
x.value = 10;

the call to value indeed calls the setter method and performs its function.

However when I built-in my component in the html code:

<body>
   <data-uint id='num'></data-uint>
</body>

and trying to access/use it like this:

x = body.children[0];
x.value = 10;

the call to value sets a new property to Element that is referenced by x, but never calls the setter method of the web component. The x however refers to the correct element on the page (my component) which I verified by calling other standard Element methods. It looks like this access method is returning a generic Element ignoring specializations.

Question:

I guess I am missing some basic concept here. How do I access html-defined component from JavaScript in a way that will allow me to use its member functions?

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

0

You are probably executing x = body.children[0]; x.value = 10; before the component is defined. Also note that unless you have declared a local variable body before that code runs, body will be undefined and you probably mean to use const x = document.body.children[0];.

Add this line of code:

const x = document.body.children[0]; 
console.log(x.constructor.name);
x.value = 10;

If that gives you HTMLElement, your component isn't defined at the time. If it's not defined yet, there is no setter to execute.

To check, you can also do console.log(x.matches(':defined'));.

To fix that, either wrap your code in a DOMContentLoaded listener, or wait for the component to be defined:

customElements
  .whenDefined('data-uint')
  .then((promise) => {
    const x = document.body.children[0]; 
    console.log(x.constructor.name);
    x.value = 10;
  });
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!