I have a div that contains a header. In this header I will write characteristics of the object I am clicking on in the page. The initial on-click trigger works fine and processes the post request to a local flask server and displays the plot in the div using plotly.js. The problem comes in when the div is collapsed by clicking on the header and then trying to click on an object in the page. The desired outcome for this second on-click event is for the data inside the ‘plot data’ div to update, and causing the header to expand if it is minimized. However, the observed behavior is that if you click on an object, triggering an on-click event while the div is hidden, it results in the following error in the dev console.
Cannot read properties of null (reading 'innerText')
referencing this line:
console.log(document.getElementById("header symbol").innerText)
The relevant code here is below:
Jquery for expanding and closing the div
$(".header").click(function () {
const $header = $(this);
//getting the next element
var $content = $header.next();
//open up the content needed - toggle the slide- if visible, slide up, if not slidedown.
$content.slideToggle(200, function () {
//execute this after slideToggle is done
//change text of header based on visibility of content div
console.log(document.getElementById("header symbol").innerText);
$header.text(function () {
//change text based on condition
return $content.is(":visible")
? `▼ ${clickedBranchName} -- ${clickedBranchFromBus} ⟶ ${clickedBranchToBus}`
: `▲ ${clickedBranchName} -- ${clickedBranchFromBus} ⟶ ${clickedBranchToBus}`;
});
});
});
Plotting the data into the ‘plot data’ div:
function plotFlow(info) {
console.log(info.object.properties.name);
const dates = topoData.data.datetime;
const flows = topoData.data.flow;
var trace1 = { x: dates, y: flows, mode: "lines", type: "scatter" };
var data = [trace1];
var layout = {
plot_bgcolor: "rgba(128,128,128,1)",
paper_bgcolor: "rgba(128,128,128,1)",
height: 350,
xaxis: {
tickmode: "auto",
nticks: 36,
},
};
var plot_div = document.getElementById("plot_data");
// plot_div.replaceChildren();
console.log(document.getElementById("header symbol").innerText);
document.getElementById(
"header symbol"
).innerText = `▼ ${clickedBranchName} -- ${clickedBranchFromBus} ⟶ ${clickedBranchToBus}`;
console.log(document.getElementById("header symbol").innerText);
Plotly.newPlot(plot_div, data, layout);
}
And finally,. index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<meta charset="utf-8" />
<title> WHO!?!?</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
body {
margin: 0;
font-family: sans-serif;
width: 100vw;
height: 100vh;
overflow: hidden;
}
.container {
position: absolute;
margin: 0;
bottom: 0px;
width: 100%;
border:1px solid #d3d3d3;
z-index: 999;
}
.container div {
width:100%;
}
.container .header {
background-color:#d3d3d3;
padding: 2px;
cursor: pointer;
font-weight: bold;
}
.container .content {
display: none;
padding : 5px;
}
</style>
</head>
<body>
<div id="app"></div>
<div id="plot_div" class="container">
<div class="header">
<span id="header symbol">▲</span>
</div>
<div id="plot_data"></div>
</div>
</div>
</body>
<script type="text/javascript" src="app.js"></script>
<script type="text/javascript">
App.renderToDOM(document.getElementById("app"));
</script>
</html>