layout | title | label | permalink | nav_order | parent |
---|---|---|---|---|---|
page |
Insert JavaScript in a Web-Component |
Script Tag |
/usage-web-components/script-tag/ |
10 |
Usage |
The script tag is has a special behavior. You will create a class extending the component, that's how you build your full component with advanced script.
To do so extend the Lego
default class:
export default class extends Lego {
…
}
Even if it's not the most recommended way it might occur that you need to access a DOM element from the script tag.
In which case the shortcut this.document
will gain you access to the DOM,
wether it's the Shadow DOM (default) or you toggled to Light DOM (overriding).
this.document
has all the methods you may expect from a document such as
querySelector
, getElementById
, querySelectorAll
…
Ex: this.document.querySelectorAll('a')