JavaScript Change SVG text from inside an img tag change text from all caps,change text from all caps

I am trying to change text of an svg item inside an img tag dynamically with jquery. I keep grabbing the wrong node, because all the boards say to change textContent, but it never alters the text. Here is my HTML:

<head>
<script type="text/javascript">
            $(window).resize(function() {
                $('.ball').height($(window).height()+"px" );
                $('.ball').width($(window).width()/6+"px");
            });
            $(function() {
                $( ".ball" ).each(function( index ) {
                    var textNode = $(this);
                    textNode = textNode.find(".changeText");
                    textNode = textNode.children().first();
                    console.log(textNode);
                    textNode.textContent = "5";
                });
            });
            $(window).trigger('resize');
        </script>
    </head>
    <body>
        <div id="svgMain">
            <img class="ball" src="ball.svg"/>
            <img class="ball" src="ball.svg"/>
            <img class="ball" src="ball.svg"/>
            <img class="ball" src="ball.svg"/>
            <img class="ball" src="ball.svg"/>
            <img class="ball" src="ball.svg"/>
        </div>
    </body>

And then here is the svg within the img tag I am trying to load dynamically:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
    <g>
        <circle cx="50%" cy="50%" r="50%"/>
        <text class="changeText" text-anchor="middle" x="50%" y="60%" font-family="Verdana" font-size="35" fill="blue" ><tspan>150</tspan>  </text>
    </g>
</svg>
Answer:1

You can't access anything inside an <img> tag from DOM. This is not part of the document, and nothing inside of it can access the document either.

For your problem, either use an <iframe> an <object> or an <embed> element to access it (plnkr),
either use and modify dataURIs (harder, slower and.. well don't do it actually).

Answer:2

Your textNode is a jQuery object. Instead of first(), take [0] to extract the DOM object from it - DOM nodes have .textContent, jQuery objects don't.

Alternately, use .text setter on the jQuery object. Both of these approaches work.

var textNode = $('svg');
textNode = textNode.find(".changeText");
textNode = textNode.children()[0];
textNode.textContent = "5";

var textNode = $('svg');
textNode = textNode.find(".changeText");
textNode = textNode.children();
textNode.text("7");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<svg height="100" width="100" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
  <g>
    <circle cx="50%" cy="50%" r="50%" />
    <text class="changeText" text-anchor="middle" x="50%" y="60%" font-family="Verdana" font-size="35" fill="blue">
      <tspan>150</tspan>
    </text>
  </g>
</svg>
Answer:3

hey i'm trying to login through a popup window. but i couldn't validate the popup and i'm currently using codeigniter. I want to validate my popup login window, which contain email and pswd fields. ...

hey i'm trying to login through a popup window. but i couldn't validate the popup and i'm currently using codeigniter. I want to validate my popup login window, which contain email and pswd fields. ...

The snippet below shows the way I've coded an onKeyPress function to call a function when the Enter key is pressed. Will this handle all (or at least most) browsers? function saveMarginKeyPress(...

The snippet below shows the way I've coded an onKeyPress function to call a function when the Enter key is pressed. Will this handle all (or at least most) browsers? function saveMarginKeyPress(...

As a functional Javascript developer with only a vague understanding of Haskell I really have a hard time to understand Haskell idioms like monads. When I look at >>= of the function instance (&...

As a functional Javascript developer with only a vague understanding of Haskell I really have a hard time to understand Haskell idioms like monads. When I look at >>= of the function instance (&...

  1. what does bind function return
  2. how does bind function work
  3. what does bind function do in socket programming
  4. what does bind function do in javascript
  5. what does bind function do in react
  6. what does bind function do

I was trying to divide a variable with type "UNDEFINED" and other variable with type "OBJECT" and I was storing the value of the division in the third parameter and when i was checking the type for ...

I was trying to divide a variable with type "UNDEFINED" and other variable with type "OBJECT" and I was storing the value of the division in the third parameter and when i was checking the type for ...

  1. variable income
  2. come up with variable cost