innerHTML not displaying in the DOM, no errors in the console

356 views Asked by At

This might be something trivial to solve, but I can't seem to figure it out. document.getElementById selects the tspan I'm trying to write data to, but nothing shows up. Code as follows. The var percentAS is set to 71, which is working fine. No errors in the console. Note this is a minimized version of my code there's actually a lot more in the document.

<body>
    <text id="data" fill="white" x="460" y="160">
        {{signups.[0]}} -
        <tspan id="percentAS"></tspan>
    </text>
</body>

<script>
    document.getElementById("percentAS").innerHTML = percentAS + "%";
</script>

I tried logging some data to the console to see what was going on:

console.log(document.getElementById("percentAS")); 

And this is returned in the error console:

tspan#percentAS.[object SVGAnimatedString]
  innerHTML: "71%"
  __proto__: SVGTSpanElementPrototype

EDIT Others have asked for more context, but I know for sure that there is nothing interfering with what I pasted above, or I would have added more context. It's just some basic sgv polygons that are a huge pain to read because they have so many points, which is why I didn't include them.

EDIT 2 It appears that my issue is unique to the Safari web browser, as I opened it in Chrome and Firefox and it works perfectly fine. I can't find any known issues with Safari in regards to this, although I've found a couple blog posts from a few years ago detailing a similar issue.

3

There are 3 answers

0
thekeegs On BEST ANSWER

Looks like it's a problem with Safari web browser itself and not the code. I switched to using jQuery to do it:

 $("#percentAS").append(percentAS + "%");

and it worked just fine in all browsers.

3
Pytth On

Your problem is that in your Javascript, the variable percentAs is not bound to anything.

If you gave it a value, such as var percentAs = 45 it would work.

EDIT: As per someone's comment. It would be nice to see where percentAs is being declared in your code since that could effect the way functions work that try to access it.

5
Zack On

This is working fine. There must be something else going wrong in your code.

var percentAS = 71;
document.getElementById("percentAS").innerHTML = percentAS + "%";
<text id="data" fill="white" x="460" y="160">{{signups.[0]}} -
    <tspan id="percentAS"></tspan>
</text>