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

9th Dec 2018, 7:05 PM
Nemanja Miletić
Nemanja Miletić - avatar
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>
9th Dec 2018, 7:21 PM
Sudarshan Rai
Sudarshan Rai - avatar
+ 4
???
10th Dec 2018, 3:51 AM
Sudarshan Rai
Sudarshan Rai - avatar
0
Nice. It worked. But now, more questions arise.
9th Dec 2018, 8:20 PM
Nemanja Miletić
Nemanja Miletić - avatar