Appended element is visible in document but not visible visually


I created an svg element and appended it to my website. Despite the fact that it’s being added (it’s visible in svg tag after inspecting website) it doesn’t appear visually on website. Why it isn’t displayed?


<svg id='timer' viewBox="0 0 300 300">
    <circle cx="150" cy="150" r="100" />


const svg = document.querySelector('#timer');

const radius = 100;
const wh = 300;

// Creating circle and appending it
const circle = document.createElement('circle');

circle.setAttribute('cx', `${wh / 2}`);
circle.setAttribute("cy", `${wh / 2}`);
circle.setAttribute("r", `${radius / 2}`);
circle.setAttribute("fill", "pink");


Here is the jsfiddle with the code:
EDIT: Updated jsfiddle link

After mucho searching and modifications, it looks like you simply need to declare your circle differently because it’s an SVG element.

const circle = document.createElementNS("", 'circle');

instead of

const circle = document.createElement('circle');
1 Like

Thank you very much! :heart: I was searching for that with less accurate keywords and couldn’t find answer to this problem.

1 Like

As I get more experienced as an actual developer, I find myself spending more and more time having to troubleshoot in areas I don’t know about, so I’ve definitely built up some skills (and persistence) in this realm.

I wish there were better ways to do troubleshooting and to teach the related skills. Alas, right now it just seems to come with lots of (frequently painful) experience :wink: Of course, asking questions, with examples of the code is one of the best ways for folks who are just getting started! :smiley: :+1:

Best wishes!