question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to get child element by id ?

See original GitHub issue

How to get a specific child element by id. In the following example I would like to get the canvas child element in order to draw something on it:

class MyElement extends LitElement {

      _render() {
        return html`
            <canvas id="draw"></canvas>
        `;
      }

}

With Polymer elements I used to get the child element with this.$.draw but it’s not working with LitElement. What is the workaround ?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

37reactions
Lodincommented, May 12, 2018

this.shadowRoot.getElementById('draw') should work

7reactions
Link2Twentycommented, May 15, 2018

I think, generally, it’s better to use this._root as it accounts for shadydom, as well as shadowdom, also you can use this.renderComplete.then in your constructor to wait until the first render is complete.

class testElement extends LitElement {
  constructor() {
    super();
    this.renderComplete.then(()=>{
      console.log(this._root.querySelector('#draw'));
    })
  }
  _render() {
    return html`<canvas id="draw"></canvas>`;
  }  
}
	
customElements.define('test-element', testElement);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Get Child Element by ID using JavaScript | bobbyhadz
To get a child element by id, use the `document.querySelector()` method to get the parent element and call the `querySelector` method on the...
Read more >
javascript get child by id - Stack Overflow
As mentioned, IDs are supposed to be unique within a document, so it's easiest to just use document.getElementById("child") . Share.
Read more >
Getting Child Elements of a Node in JavaScript
First, select the #menu element by using the getElementById() method. · Second, get the first child element by using the firstElementChild property.
Read more >
HTML DOM Element children Property - W3Schools
Get a collection of the <body> element's children: ... The children property returns a collection of an element's child elements. The children property ......
Read more >
Get child element in JavaScript HTML DOM - CodeSpeedy
In JavaScript HTML DOM, we can easily get the child element of a parent element using the childNodes property. In the above HTML,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found