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

186
Views
How can I modify properties of an element inside an html template?

I have an html template stored in a variable:

const myTemplate = html`
    <my-component>
    </my-component>
`

and I would like to add a property to myTemplate.

Something like

myTemplate.foo = "bar"

But this references the template rather than the element; how can I isolate the element to modify it?

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

0

For this kind of cases, you would normally use a function that returns the template.

const myTemplate = (props) => {
  return html`<my-component foo=${props.foo} .bar=${props.bar}></mycomponent>`;
};
const instance = myTemplate({foo: 'some value', bar: 3});
const otherInstance = myTemplate({foo: 'other value', bar: 42});
about 4 years ago · Juan Pablo Isaza Report

0

As far as I know, you can't, at least not how you are trying (at least with public APIs).

The best option would be to simply render it with the option already there:

const myTemplate = html`
    <my-component .foo=${'bar'}>
    </my-component>
`;

That should be the approach you use 99.9% of the time. If for some reason your really, really can't do that, you'd need to go ahead and render the template to some placeholder element and then modify it with the DOM:

const div = document.createElement('div');
render(myTemplate, div);

div.querySelector('my-component').foo = 'bar';
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!