1

I am trying to use plotly java script click event in angular 2 project but it is returning the below error

Property 'on' does not exist on type 'HTMLElement'.

let myPlot = document.getElementById('donutChartId');
myPlot.on('plotly_click', function(data){
console.log(data.points[0].label);
});

Angular build is failing because of this.

2 Answers 2

1

.on() is a jquery method

If you are using Jquery in your application you may create a jquery object from selected element to use this method.

let myPlot = document.getElementById('donutChartId');

to

let myPlot = $(document.getElementById('donutChartId'));

Hope this helps!!

Sign up to request clarification or add additional context in comments.

2 Comments

I tried that before but it says "Cannot find name '$"
Have you added types for jquery? you should have types installed something like this should be in your depenedcies for types "@types/jquery": "1.10.31",', and also actual jquery library should be part of your application.
0

Think it is more of a compilation error. But your code will work. If you are concerned on the error. You can create a child class for ES6 HTMLElement class. Add the below code above in your component.

class CustomHTMLElement extends HTMLElement {
  constructor() {
    super();
  }
  on(event_type, cb) {
  }
}

And type cast in element selection

let myPlot = <CustomHTMLElement>document.getElementById('donutChartId');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.