0
Can I access SVG element with JavaScript?
I was trying to get element from SVG using JS DOM methods. I failed. I searched for a whiile and I found this code. Again, it returned error. This is not the only code I've tryed. All of them returned error just like this one. What is this about? Is it even possible to acesss SVG elements like this? https://code.sololearn.com/WTGckhcYE18m/?ref=app
3 Answers
+ 7
// Try like this one
<svg id="svgObject" height="40px" width="40px" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 800 800"> <rect id="svgItem" width="800" height="800" fill="red"></rect> <circle cx="400" cy="400" r="400" fill=""></circle> </svg>
</body>
<script>
window.onload=function(){
var svgItem =document.getElementById("svgObject"); svgItem.style.fill ="green"
}
</script>
+ 4
???
0
Nice. It worked. But now, more questions arise.