Thomas
Thomas

Reputation: 5911

Web component, how to get an element by Id?

I want to grab an element by its ID. My template looks like this:

<template>
    <h2>Welcome</h2>    
    <div id="toGrab"></div>
</template>

I tried using jQuery but the below snippet doesn't work:

$('#toGrab').dosomething();

Any idea?

Upvotes: 3

Views: 5482

Answers (3)

Jack Markiewicz
Jack Markiewicz

Reputation: 31

In Polymer 3:

ready() {
  super.ready();
  const x = this.shadowRoot.getElementById('divID');
}

Upvotes: 3

Olivier Langelaar
Olivier Langelaar

Reputation: 186

also...

var div = this.$$("#toGrab");

Upvotes: 0

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657138

If you code is inside the Polymer element, just do

var div = this.$.toGrab;

Upvotes: 4

Related Questions