I am trying to manipulate the SVG file based on this post document.getElementById results in null where moving svg into object tag and below are my files.
index.html
<body>
<object data="../svg/barplot.svg" alt='bar-graph' type="image/svg+xml" id="barplot" width="800"
height="800">
</object>
</body>
script.js
window.addEventListener("load", function () {
var barplot = document.getElementById("barplot");
console.log(barplot);
var svgDoc = barplot.contentDocument;
console.log(svgDoc);
When I use the VS Code live server, the output of svgDoc is the SVG file itself and everything works perferct.
Live server (barplot)
<object data="../svg/barplot.svg" alt="bar-graph" type="image/svg+xml" id="barplot" width="800" height="800">
</object>
Live server (svgDoc)
#document
<svg>
svg content
</svg>
However, if I use open index.html in the terminal, the console output of svgDoc is null.
Open index.html (barplot)
<object data="../svg/barplot.svg" alt="bar-graph" type="image/svg+xml" id="barplot" width="800" height="800">
</object>
Open index.html (svgDoc)
null
For future development, I have to use the open index.html method. Could someone help with this issue? Thanks