Surendar
Surendar

Reputation: 153

Uncaught Error: DOM element provided is null or undefined in plotly JS

This is my Code:

frequencywindow.webContents.executeJavaScript(`graphDiv = document.getElementById("myDiv");`)
var traceA = {
  x: [1, 2, 3, 4, 16, 17, 26],
  y: [1, 40, 9, 60, 4, 20, 10],
  type: 'scatter'
};
var data = [traceA];
var layout = {
  title:'A Line Chart in Plotly'
};
Plotly.plot( graphDiv, data, layout );

I am using plotly in my electron application. I am getting an error like "Uncaught Error: DOM element provided is null or undefined". I am new to plotly. Can somebody help me?

Upvotes: 4

Views: 3437

Answers (1)

Jakub Holan
Jakub Holan

Reputation: 421

Solution:

Place the Javascript code (or reference to it) at the end of the HTML body.

Explanation:

Uncaught Error: DOM element provided is null or undefined means that the HTML element with id='myDiv' is not present on the HTML page at the time when the Javascript code is being executed.

By placing the Javascript code at the end of the HTML body, you let the whole HTML page load before calling the code, and thus, the code can "see" the HTML element with id='myDiv'.

Upvotes: 6

Related Questions