1

I have a template code like this:

<img id ="canvas" src="../images/carsimage.jpg" />

I am trying to pass this element to a function by making something like getElementById in javascript. But I am getting an error with 'this.$.canvas'

initDraw(this.$.canvas);

initDraw(canvas) {
}
alditis
  • 4,633
  • 3
  • 49
  • 76
  • 1
    `initDraw(document.getElementById("canvas"));` – tymeJV Oct 25 '17 at 20:50
  • By *template code*, do you mean that HTML/code is inside your Polymer template? What error are you getting with `this.$.canvas`? Do you have a fiddle to reproduce this (e.g., with Codepen)? – tony19 Oct 26 '17 at 06:37
  • this is really bad question. Are you using shadow dom or not, what is the error. Where is the problem.. Best to do is to create a fiddle. – Kuba Šimonovský Oct 26 '17 at 07:08

1 Answers1

1

You should define a better question and provide a codepen/fiddle/whatever, but assuming you want to call it from a component, you forgot to call the initDraw function using this. In Polymer v2 would be something like this:

ready() {
   super.ready();
   this.initDraw(this.$.canvas);    //here call this.functionName
}

initDraw(canvas) {
   console.log('canvas = ', canvas);
}
Arnau
  • 357
  • 1
  • 10