Here is some sample HTML and CSS that includes an SVG as a background
:
<div class="box"></div>
.box {
width: 300px;
height: 200px;
background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg"><rect id="rect" width="100" height="100" fill="red"><animate attributeName="x" from="0" to="400" dur="6s" repeatCount="indefinite"/></rect></svg>');
}
I am attempting to use JavaScript to target the element with #rect
like this:
// Ensure the SVG has loaded.
window.onload = function() {
// ...however, this still returns null
console.log(document.getElementById("rect"));
}
Despite my efforts, the console displays null
. Is there a different method to reference this <rect>
using JavaScript?