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

118
Views
Getting tag based on ID of element inside shadowroot

I am having the below html that I render in a lit component:

render() {
    return html`
          <div class="table">
            <div id="col">
              <p>testing this component</p>
</div>
</div>`

through the below constructor I'm calling a resize handler:

constructor() {
    super();
    window.addEventListener('resize', this._handleResize);
  }

the handleresize method is as below:

private _handleResize = () =>{
  var some_id = document.getElementById('col');
  var tag = some_id.tagName; ///some_id is coming out as null
}

I am trying to get the tag of 'col' ID and it's throwing me null. Can someone tell what is the mistake here?

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

0

Instead of querying for the element, you should use a reference:

Referencing rendered DOM

import {ref} from 'lit/directives/ref.js';

[...]

  colRef = createRef();

[...]

  render() {
    return html`
      <div class="table">
        <div id="col" ${ref(this.colRef)}>
          <p>testing this component</p>
        </div>
      </div>`
  }

  private _handleResize = () =>{
    const col = this.colRef.value!;
    const tag = col.tagName;
  }

I also suggest to using const and let instead of var. Furthermore you should probably use ResizeObserver instead of an eventlistener on window. Also don't forget to unregister your eventlisteners.

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!