Change text of an Svg











up vote
0
down vote

favorite
1












Is it possible to make changes to the text of an SVG with js?



So if I have some SVG:s. Later it should be a booking system. So there is a calendar and every bookable time I have an SVG of an calendar (like MacOS) and in every calendar SVG should later be displaying the correct date. So is it possible to make an SVG variable and then change the content?



<?xml version="1.0" standalone="no"?>
<!-- Generator: Gravit.io -->
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="isolation:isolate" viewBox="0 0 200 200" width="200" height="200">
<defs>
<clipPath id="_clipPath_BTzJAWdHJZVUGGYrrUDwSyNPa5Ujloqh">
<rect width="200" height="200"/>
</clipPath>
</defs>
<g clip-path="url(#_clipPath_BTzJAWdHJZVUGGYrrUDwSyNPa5Ujloqh)">
<g>
<rect x="41.4" y="40.189" width="117.199" height="120.236" transform="matrix(0.951057,-0.309017,0.309017,0.951057,-26.102184,35.811068)" fill="rgb(28,27,27)"/>
<rect x="41.4" y="38.233" width="117.199" height="120.236" transform="matrix(0.961262,-0.275637,0.275637,0.961262,-23.235267,31.373671)" fill="rgb(57,57,57)"/>
<rect x="41.4" y="38.233" width="117.199" height="120.236" transform="matrix(0.978148,-0.207912,0.207912,0.978148,-18.262998,22.940365)" fill="rgb(81,80,80)"/>
<rect x="41.4" y="36.276" width="117.199" height="120.236" transform="matrix(0.984808,-0.173648,0.173648,0.984808,-15.219469,18.829264)" fill="rgb(149,149,149)"/>
<rect x="41.4" y="34.32" width="117.199" height="120.236" transform="matrix(0.990268,-0.139173,0.139173,0.990268,-12.170036,14.836374)" fill="rgb(208,208,208)"/>
<rect x="41.4" y="32.364" width="117.199" height="120.236" transform="matrix(0.994522,-0.104528,0.104528,0.994522,-9.11916,10.959471)" fill="rgb(255,255,255)"/>
</g>
<rect x="36.838" y="32.603" width="117.199" height="32.94" transform="matrix(0.994522,-0.104528,0.104528,0.994522,-4.606717,10.24477)" fill="rgb(255,63,56)"/>
<g transform="matrix(0.994522,-0.104528,0.104528,0.994522,35.499851,38.859638)">
<text transform="matrix(1,0,0,1,32.049347,25.853783)" style="font-family:'Open Sans';font-weight:400;font-size:24px;font-style:normal;word-spacing:5;fill:#ffffff;stroke:none;">NOV</text>
</g>
<g transform="matrix(0.994522,-0.104528,0.104528,0.994522,39.028839,71.380565)">
<text transform="matrix(1,0,0,1,17.498417,70.832115)" style="font-family:'Open Sans';font-weight:400;font-size:70px;font-style:normal;fill:#000000;stroke:none;">16</text>
</g>
</g>
</svg>


And now, is it possible to change the content of the text?










share|improve this question




























    up vote
    0
    down vote

    favorite
    1












    Is it possible to make changes to the text of an SVG with js?



    So if I have some SVG:s. Later it should be a booking system. So there is a calendar and every bookable time I have an SVG of an calendar (like MacOS) and in every calendar SVG should later be displaying the correct date. So is it possible to make an SVG variable and then change the content?



    <?xml version="1.0" standalone="no"?>
    <!-- Generator: Gravit.io -->
    <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="isolation:isolate" viewBox="0 0 200 200" width="200" height="200">
    <defs>
    <clipPath id="_clipPath_BTzJAWdHJZVUGGYrrUDwSyNPa5Ujloqh">
    <rect width="200" height="200"/>
    </clipPath>
    </defs>
    <g clip-path="url(#_clipPath_BTzJAWdHJZVUGGYrrUDwSyNPa5Ujloqh)">
    <g>
    <rect x="41.4" y="40.189" width="117.199" height="120.236" transform="matrix(0.951057,-0.309017,0.309017,0.951057,-26.102184,35.811068)" fill="rgb(28,27,27)"/>
    <rect x="41.4" y="38.233" width="117.199" height="120.236" transform="matrix(0.961262,-0.275637,0.275637,0.961262,-23.235267,31.373671)" fill="rgb(57,57,57)"/>
    <rect x="41.4" y="38.233" width="117.199" height="120.236" transform="matrix(0.978148,-0.207912,0.207912,0.978148,-18.262998,22.940365)" fill="rgb(81,80,80)"/>
    <rect x="41.4" y="36.276" width="117.199" height="120.236" transform="matrix(0.984808,-0.173648,0.173648,0.984808,-15.219469,18.829264)" fill="rgb(149,149,149)"/>
    <rect x="41.4" y="34.32" width="117.199" height="120.236" transform="matrix(0.990268,-0.139173,0.139173,0.990268,-12.170036,14.836374)" fill="rgb(208,208,208)"/>
    <rect x="41.4" y="32.364" width="117.199" height="120.236" transform="matrix(0.994522,-0.104528,0.104528,0.994522,-9.11916,10.959471)" fill="rgb(255,255,255)"/>
    </g>
    <rect x="36.838" y="32.603" width="117.199" height="32.94" transform="matrix(0.994522,-0.104528,0.104528,0.994522,-4.606717,10.24477)" fill="rgb(255,63,56)"/>
    <g transform="matrix(0.994522,-0.104528,0.104528,0.994522,35.499851,38.859638)">
    <text transform="matrix(1,0,0,1,32.049347,25.853783)" style="font-family:'Open Sans';font-weight:400;font-size:24px;font-style:normal;word-spacing:5;fill:#ffffff;stroke:none;">NOV</text>
    </g>
    <g transform="matrix(0.994522,-0.104528,0.104528,0.994522,39.028839,71.380565)">
    <text transform="matrix(1,0,0,1,17.498417,70.832115)" style="font-family:'Open Sans';font-weight:400;font-size:70px;font-style:normal;fill:#000000;stroke:none;">16</text>
    </g>
    </g>
    </svg>


    And now, is it possible to change the content of the text?










    share|improve this question


























      up vote
      0
      down vote

      favorite
      1









      up vote
      0
      down vote

      favorite
      1






      1





      Is it possible to make changes to the text of an SVG with js?



      So if I have some SVG:s. Later it should be a booking system. So there is a calendar and every bookable time I have an SVG of an calendar (like MacOS) and in every calendar SVG should later be displaying the correct date. So is it possible to make an SVG variable and then change the content?



      <?xml version="1.0" standalone="no"?>
      <!-- Generator: Gravit.io -->
      <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="isolation:isolate" viewBox="0 0 200 200" width="200" height="200">
      <defs>
      <clipPath id="_clipPath_BTzJAWdHJZVUGGYrrUDwSyNPa5Ujloqh">
      <rect width="200" height="200"/>
      </clipPath>
      </defs>
      <g clip-path="url(#_clipPath_BTzJAWdHJZVUGGYrrUDwSyNPa5Ujloqh)">
      <g>
      <rect x="41.4" y="40.189" width="117.199" height="120.236" transform="matrix(0.951057,-0.309017,0.309017,0.951057,-26.102184,35.811068)" fill="rgb(28,27,27)"/>
      <rect x="41.4" y="38.233" width="117.199" height="120.236" transform="matrix(0.961262,-0.275637,0.275637,0.961262,-23.235267,31.373671)" fill="rgb(57,57,57)"/>
      <rect x="41.4" y="38.233" width="117.199" height="120.236" transform="matrix(0.978148,-0.207912,0.207912,0.978148,-18.262998,22.940365)" fill="rgb(81,80,80)"/>
      <rect x="41.4" y="36.276" width="117.199" height="120.236" transform="matrix(0.984808,-0.173648,0.173648,0.984808,-15.219469,18.829264)" fill="rgb(149,149,149)"/>
      <rect x="41.4" y="34.32" width="117.199" height="120.236" transform="matrix(0.990268,-0.139173,0.139173,0.990268,-12.170036,14.836374)" fill="rgb(208,208,208)"/>
      <rect x="41.4" y="32.364" width="117.199" height="120.236" transform="matrix(0.994522,-0.104528,0.104528,0.994522,-9.11916,10.959471)" fill="rgb(255,255,255)"/>
      </g>
      <rect x="36.838" y="32.603" width="117.199" height="32.94" transform="matrix(0.994522,-0.104528,0.104528,0.994522,-4.606717,10.24477)" fill="rgb(255,63,56)"/>
      <g transform="matrix(0.994522,-0.104528,0.104528,0.994522,35.499851,38.859638)">
      <text transform="matrix(1,0,0,1,32.049347,25.853783)" style="font-family:'Open Sans';font-weight:400;font-size:24px;font-style:normal;word-spacing:5;fill:#ffffff;stroke:none;">NOV</text>
      </g>
      <g transform="matrix(0.994522,-0.104528,0.104528,0.994522,39.028839,71.380565)">
      <text transform="matrix(1,0,0,1,17.498417,70.832115)" style="font-family:'Open Sans';font-weight:400;font-size:70px;font-style:normal;fill:#000000;stroke:none;">16</text>
      </g>
      </g>
      </svg>


      And now, is it possible to change the content of the text?










      share|improve this question















      Is it possible to make changes to the text of an SVG with js?



      So if I have some SVG:s. Later it should be a booking system. So there is a calendar and every bookable time I have an SVG of an calendar (like MacOS) and in every calendar SVG should later be displaying the correct date. So is it possible to make an SVG variable and then change the content?



      <?xml version="1.0" standalone="no"?>
      <!-- Generator: Gravit.io -->
      <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="isolation:isolate" viewBox="0 0 200 200" width="200" height="200">
      <defs>
      <clipPath id="_clipPath_BTzJAWdHJZVUGGYrrUDwSyNPa5Ujloqh">
      <rect width="200" height="200"/>
      </clipPath>
      </defs>
      <g clip-path="url(#_clipPath_BTzJAWdHJZVUGGYrrUDwSyNPa5Ujloqh)">
      <g>
      <rect x="41.4" y="40.189" width="117.199" height="120.236" transform="matrix(0.951057,-0.309017,0.309017,0.951057,-26.102184,35.811068)" fill="rgb(28,27,27)"/>
      <rect x="41.4" y="38.233" width="117.199" height="120.236" transform="matrix(0.961262,-0.275637,0.275637,0.961262,-23.235267,31.373671)" fill="rgb(57,57,57)"/>
      <rect x="41.4" y="38.233" width="117.199" height="120.236" transform="matrix(0.978148,-0.207912,0.207912,0.978148,-18.262998,22.940365)" fill="rgb(81,80,80)"/>
      <rect x="41.4" y="36.276" width="117.199" height="120.236" transform="matrix(0.984808,-0.173648,0.173648,0.984808,-15.219469,18.829264)" fill="rgb(149,149,149)"/>
      <rect x="41.4" y="34.32" width="117.199" height="120.236" transform="matrix(0.990268,-0.139173,0.139173,0.990268,-12.170036,14.836374)" fill="rgb(208,208,208)"/>
      <rect x="41.4" y="32.364" width="117.199" height="120.236" transform="matrix(0.994522,-0.104528,0.104528,0.994522,-9.11916,10.959471)" fill="rgb(255,255,255)"/>
      </g>
      <rect x="36.838" y="32.603" width="117.199" height="32.94" transform="matrix(0.994522,-0.104528,0.104528,0.994522,-4.606717,10.24477)" fill="rgb(255,63,56)"/>
      <g transform="matrix(0.994522,-0.104528,0.104528,0.994522,35.499851,38.859638)">
      <text transform="matrix(1,0,0,1,32.049347,25.853783)" style="font-family:'Open Sans';font-weight:400;font-size:24px;font-style:normal;word-spacing:5;fill:#ffffff;stroke:none;">NOV</text>
      </g>
      <g transform="matrix(0.994522,-0.104528,0.104528,0.994522,39.028839,71.380565)">
      <text transform="matrix(1,0,0,1,17.498417,70.832115)" style="font-family:'Open Sans';font-weight:400;font-size:70px;font-style:normal;fill:#000000;stroke:none;">16</text>
      </g>
      </g>
      </svg>


      And now, is it possible to change the content of the text?







      javascript svg dynamic






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 17 at 17:06









      ProgrammerPer

      393411




      393411










      asked Nov 17 at 14:56









      Svenwas3f

      32




      32
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          In the case you need several SVG elements like this you can do the following:
          For clarity I've simplified a lot your code.






          let svgs = document.querySelectorAll(".svg")


          function changeDate(svg,month,day){
          svg.querySelectorAll("text")[0].textContent = month;
          svg.querySelectorAll("text")[1].textContent = day;
          }

          changeDate(svgs[0],"DEC","25");
          changeDate(svgs[1],"JAN","6");

          .month{font-family:'Open Sans';font-weight:400;font-size:20px;font-style:normal;word-spacing:5;fill:white;stroke:none;}
          .day{font-family:'Open Sans';font-weight:400;font-size:70px;font-style:normal;fill:#000000;stroke:none;}
          text{text-anchor:middle;}

          <svg viewBox="0 0 200 200" width="200" height="200">
          <g id="bg">
          <g>
          <rect x="41.4" y="40.189" width="117.199" height="120.236" transform="matrix(0.951057,-0.309017,0.309017,0.951057,-26.102184,35.811068)" fill="rgb(28,27,27)"/>
          <rect x="41.4" y="38.233" width="117.199" height="120.236" transform="matrix(0.961262,-0.275637,0.275637,0.961262,-23.235267,31.373671)" fill="rgb(57,57,57)"/>
          <rect x="41.4" y="38.233" width="117.199" height="120.236" transform="matrix(0.978148,-0.207912,0.207912,0.978148,-18.262998,22.940365)" fill="rgb(81,80,80)"/>
          <rect x="41.4" y="36.276" width="117.199" height="120.236" transform="matrix(0.984808,-0.173648,0.173648,0.984808,-15.219469,18.829264)" fill="rgb(149,149,149)"/>
          <rect x="41.4" y="34.32" width="117.199" height="120.236" transform="matrix(0.990268,-0.139173,0.139173,0.990268,-12.170036,14.836374)" fill="rgb(208,208,208)"/>
          <rect x="41.4" y="32.364" width="117.199" height="120.236" transform="matrix(0.994522,-0.104528,0.104528,0.994522,-9.11916,10.959471)" fill="rgb(255,255,255)"/>
          </g>
          <rect x="36.838" y="32.603" width="117.199" height="32.94" transform="matrix(0.994522,-0.104528,0.104528,0.994522,-4.606717,10.24477)" fill="rgb(255,63,56)"/>
          </g>

          </svg>

          <svg class="svg" viewBox="0 0 200 200" width="200" height="200">
          <use xlink:href="#bg" />
          <g transform="matrix(0.994522,-0.104528,0.104528,0.994522,95,57)">
          <text class="month" >NOV</text>
          <text class="day" x="0" y="75">16</text>
          </g>
          </svg>

          <svg class="svg" viewBox="0 0 200 200" width="200" height="200">
          <use xlink:href="#bg" />
          <g transform="matrix(0.994522,-0.104528,0.104528,0.994522,95,57)">
          <text class="month" >NOV</text>
          <text class="day" x="0" y="75">16</text>
          </g>
          </svg>








          share|improve this answer



















          • 1




            Thank you! That is exactly what I wanted! You helped me so much!
            – Svenwas3f
            Nov 17 at 16:56


















          up vote
          0
          down vote













          this code worked for me, use your desired function (date or time) instead of New Text.



          var list = document.getElementsByTagName("g")[0]; 
          var textNode = list.getElementsByTagName("text")[0].children[0];
          textNode.textContent = "New Text";





          share|improve this answer





















          • So but by a table I may have on the first date no bookable time but in the next week it is possible so that with the [0] wouldn't work very well
            – Svenwas3f
            Nov 17 at 15:31











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53352375%2fchange-text-of-an-svg%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          In the case you need several SVG elements like this you can do the following:
          For clarity I've simplified a lot your code.






          let svgs = document.querySelectorAll(".svg")


          function changeDate(svg,month,day){
          svg.querySelectorAll("text")[0].textContent = month;
          svg.querySelectorAll("text")[1].textContent = day;
          }

          changeDate(svgs[0],"DEC","25");
          changeDate(svgs[1],"JAN","6");

          .month{font-family:'Open Sans';font-weight:400;font-size:20px;font-style:normal;word-spacing:5;fill:white;stroke:none;}
          .day{font-family:'Open Sans';font-weight:400;font-size:70px;font-style:normal;fill:#000000;stroke:none;}
          text{text-anchor:middle;}

          <svg viewBox="0 0 200 200" width="200" height="200">
          <g id="bg">
          <g>
          <rect x="41.4" y="40.189" width="117.199" height="120.236" transform="matrix(0.951057,-0.309017,0.309017,0.951057,-26.102184,35.811068)" fill="rgb(28,27,27)"/>
          <rect x="41.4" y="38.233" width="117.199" height="120.236" transform="matrix(0.961262,-0.275637,0.275637,0.961262,-23.235267,31.373671)" fill="rgb(57,57,57)"/>
          <rect x="41.4" y="38.233" width="117.199" height="120.236" transform="matrix(0.978148,-0.207912,0.207912,0.978148,-18.262998,22.940365)" fill="rgb(81,80,80)"/>
          <rect x="41.4" y="36.276" width="117.199" height="120.236" transform="matrix(0.984808,-0.173648,0.173648,0.984808,-15.219469,18.829264)" fill="rgb(149,149,149)"/>
          <rect x="41.4" y="34.32" width="117.199" height="120.236" transform="matrix(0.990268,-0.139173,0.139173,0.990268,-12.170036,14.836374)" fill="rgb(208,208,208)"/>
          <rect x="41.4" y="32.364" width="117.199" height="120.236" transform="matrix(0.994522,-0.104528,0.104528,0.994522,-9.11916,10.959471)" fill="rgb(255,255,255)"/>
          </g>
          <rect x="36.838" y="32.603" width="117.199" height="32.94" transform="matrix(0.994522,-0.104528,0.104528,0.994522,-4.606717,10.24477)" fill="rgb(255,63,56)"/>
          </g>

          </svg>

          <svg class="svg" viewBox="0 0 200 200" width="200" height="200">
          <use xlink:href="#bg" />
          <g transform="matrix(0.994522,-0.104528,0.104528,0.994522,95,57)">
          <text class="month" >NOV</text>
          <text class="day" x="0" y="75">16</text>
          </g>
          </svg>

          <svg class="svg" viewBox="0 0 200 200" width="200" height="200">
          <use xlink:href="#bg" />
          <g transform="matrix(0.994522,-0.104528,0.104528,0.994522,95,57)">
          <text class="month" >NOV</text>
          <text class="day" x="0" y="75">16</text>
          </g>
          </svg>








          share|improve this answer



















          • 1




            Thank you! That is exactly what I wanted! You helped me so much!
            – Svenwas3f
            Nov 17 at 16:56















          up vote
          0
          down vote



          accepted










          In the case you need several SVG elements like this you can do the following:
          For clarity I've simplified a lot your code.






          let svgs = document.querySelectorAll(".svg")


          function changeDate(svg,month,day){
          svg.querySelectorAll("text")[0].textContent = month;
          svg.querySelectorAll("text")[1].textContent = day;
          }

          changeDate(svgs[0],"DEC","25");
          changeDate(svgs[1],"JAN","6");

          .month{font-family:'Open Sans';font-weight:400;font-size:20px;font-style:normal;word-spacing:5;fill:white;stroke:none;}
          .day{font-family:'Open Sans';font-weight:400;font-size:70px;font-style:normal;fill:#000000;stroke:none;}
          text{text-anchor:middle;}

          <svg viewBox="0 0 200 200" width="200" height="200">
          <g id="bg">
          <g>
          <rect x="41.4" y="40.189" width="117.199" height="120.236" transform="matrix(0.951057,-0.309017,0.309017,0.951057,-26.102184,35.811068)" fill="rgb(28,27,27)"/>
          <rect x="41.4" y="38.233" width="117.199" height="120.236" transform="matrix(0.961262,-0.275637,0.275637,0.961262,-23.235267,31.373671)" fill="rgb(57,57,57)"/>
          <rect x="41.4" y="38.233" width="117.199" height="120.236" transform="matrix(0.978148,-0.207912,0.207912,0.978148,-18.262998,22.940365)" fill="rgb(81,80,80)"/>
          <rect x="41.4" y="36.276" width="117.199" height="120.236" transform="matrix(0.984808,-0.173648,0.173648,0.984808,-15.219469,18.829264)" fill="rgb(149,149,149)"/>
          <rect x="41.4" y="34.32" width="117.199" height="120.236" transform="matrix(0.990268,-0.139173,0.139173,0.990268,-12.170036,14.836374)" fill="rgb(208,208,208)"/>
          <rect x="41.4" y="32.364" width="117.199" height="120.236" transform="matrix(0.994522,-0.104528,0.104528,0.994522,-9.11916,10.959471)" fill="rgb(255,255,255)"/>
          </g>
          <rect x="36.838" y="32.603" width="117.199" height="32.94" transform="matrix(0.994522,-0.104528,0.104528,0.994522,-4.606717,10.24477)" fill="rgb(255,63,56)"/>
          </g>

          </svg>

          <svg class="svg" viewBox="0 0 200 200" width="200" height="200">
          <use xlink:href="#bg" />
          <g transform="matrix(0.994522,-0.104528,0.104528,0.994522,95,57)">
          <text class="month" >NOV</text>
          <text class="day" x="0" y="75">16</text>
          </g>
          </svg>

          <svg class="svg" viewBox="0 0 200 200" width="200" height="200">
          <use xlink:href="#bg" />
          <g transform="matrix(0.994522,-0.104528,0.104528,0.994522,95,57)">
          <text class="month" >NOV</text>
          <text class="day" x="0" y="75">16</text>
          </g>
          </svg>








          share|improve this answer



















          • 1




            Thank you! That is exactly what I wanted! You helped me so much!
            – Svenwas3f
            Nov 17 at 16:56













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          In the case you need several SVG elements like this you can do the following:
          For clarity I've simplified a lot your code.






          let svgs = document.querySelectorAll(".svg")


          function changeDate(svg,month,day){
          svg.querySelectorAll("text")[0].textContent = month;
          svg.querySelectorAll("text")[1].textContent = day;
          }

          changeDate(svgs[0],"DEC","25");
          changeDate(svgs[1],"JAN","6");

          .month{font-family:'Open Sans';font-weight:400;font-size:20px;font-style:normal;word-spacing:5;fill:white;stroke:none;}
          .day{font-family:'Open Sans';font-weight:400;font-size:70px;font-style:normal;fill:#000000;stroke:none;}
          text{text-anchor:middle;}

          <svg viewBox="0 0 200 200" width="200" height="200">
          <g id="bg">
          <g>
          <rect x="41.4" y="40.189" width="117.199" height="120.236" transform="matrix(0.951057,-0.309017,0.309017,0.951057,-26.102184,35.811068)" fill="rgb(28,27,27)"/>
          <rect x="41.4" y="38.233" width="117.199" height="120.236" transform="matrix(0.961262,-0.275637,0.275637,0.961262,-23.235267,31.373671)" fill="rgb(57,57,57)"/>
          <rect x="41.4" y="38.233" width="117.199" height="120.236" transform="matrix(0.978148,-0.207912,0.207912,0.978148,-18.262998,22.940365)" fill="rgb(81,80,80)"/>
          <rect x="41.4" y="36.276" width="117.199" height="120.236" transform="matrix(0.984808,-0.173648,0.173648,0.984808,-15.219469,18.829264)" fill="rgb(149,149,149)"/>
          <rect x="41.4" y="34.32" width="117.199" height="120.236" transform="matrix(0.990268,-0.139173,0.139173,0.990268,-12.170036,14.836374)" fill="rgb(208,208,208)"/>
          <rect x="41.4" y="32.364" width="117.199" height="120.236" transform="matrix(0.994522,-0.104528,0.104528,0.994522,-9.11916,10.959471)" fill="rgb(255,255,255)"/>
          </g>
          <rect x="36.838" y="32.603" width="117.199" height="32.94" transform="matrix(0.994522,-0.104528,0.104528,0.994522,-4.606717,10.24477)" fill="rgb(255,63,56)"/>
          </g>

          </svg>

          <svg class="svg" viewBox="0 0 200 200" width="200" height="200">
          <use xlink:href="#bg" />
          <g transform="matrix(0.994522,-0.104528,0.104528,0.994522,95,57)">
          <text class="month" >NOV</text>
          <text class="day" x="0" y="75">16</text>
          </g>
          </svg>

          <svg class="svg" viewBox="0 0 200 200" width="200" height="200">
          <use xlink:href="#bg" />
          <g transform="matrix(0.994522,-0.104528,0.104528,0.994522,95,57)">
          <text class="month" >NOV</text>
          <text class="day" x="0" y="75">16</text>
          </g>
          </svg>








          share|improve this answer














          In the case you need several SVG elements like this you can do the following:
          For clarity I've simplified a lot your code.






          let svgs = document.querySelectorAll(".svg")


          function changeDate(svg,month,day){
          svg.querySelectorAll("text")[0].textContent = month;
          svg.querySelectorAll("text")[1].textContent = day;
          }

          changeDate(svgs[0],"DEC","25");
          changeDate(svgs[1],"JAN","6");

          .month{font-family:'Open Sans';font-weight:400;font-size:20px;font-style:normal;word-spacing:5;fill:white;stroke:none;}
          .day{font-family:'Open Sans';font-weight:400;font-size:70px;font-style:normal;fill:#000000;stroke:none;}
          text{text-anchor:middle;}

          <svg viewBox="0 0 200 200" width="200" height="200">
          <g id="bg">
          <g>
          <rect x="41.4" y="40.189" width="117.199" height="120.236" transform="matrix(0.951057,-0.309017,0.309017,0.951057,-26.102184,35.811068)" fill="rgb(28,27,27)"/>
          <rect x="41.4" y="38.233" width="117.199" height="120.236" transform="matrix(0.961262,-0.275637,0.275637,0.961262,-23.235267,31.373671)" fill="rgb(57,57,57)"/>
          <rect x="41.4" y="38.233" width="117.199" height="120.236" transform="matrix(0.978148,-0.207912,0.207912,0.978148,-18.262998,22.940365)" fill="rgb(81,80,80)"/>
          <rect x="41.4" y="36.276" width="117.199" height="120.236" transform="matrix(0.984808,-0.173648,0.173648,0.984808,-15.219469,18.829264)" fill="rgb(149,149,149)"/>
          <rect x="41.4" y="34.32" width="117.199" height="120.236" transform="matrix(0.990268,-0.139173,0.139173,0.990268,-12.170036,14.836374)" fill="rgb(208,208,208)"/>
          <rect x="41.4" y="32.364" width="117.199" height="120.236" transform="matrix(0.994522,-0.104528,0.104528,0.994522,-9.11916,10.959471)" fill="rgb(255,255,255)"/>
          </g>
          <rect x="36.838" y="32.603" width="117.199" height="32.94" transform="matrix(0.994522,-0.104528,0.104528,0.994522,-4.606717,10.24477)" fill="rgb(255,63,56)"/>
          </g>

          </svg>

          <svg class="svg" viewBox="0 0 200 200" width="200" height="200">
          <use xlink:href="#bg" />
          <g transform="matrix(0.994522,-0.104528,0.104528,0.994522,95,57)">
          <text class="month" >NOV</text>
          <text class="day" x="0" y="75">16</text>
          </g>
          </svg>

          <svg class="svg" viewBox="0 0 200 200" width="200" height="200">
          <use xlink:href="#bg" />
          <g transform="matrix(0.994522,-0.104528,0.104528,0.994522,95,57)">
          <text class="month" >NOV</text>
          <text class="day" x="0" y="75">16</text>
          </g>
          </svg>








          let svgs = document.querySelectorAll(".svg")


          function changeDate(svg,month,day){
          svg.querySelectorAll("text")[0].textContent = month;
          svg.querySelectorAll("text")[1].textContent = day;
          }

          changeDate(svgs[0],"DEC","25");
          changeDate(svgs[1],"JAN","6");

          .month{font-family:'Open Sans';font-weight:400;font-size:20px;font-style:normal;word-spacing:5;fill:white;stroke:none;}
          .day{font-family:'Open Sans';font-weight:400;font-size:70px;font-style:normal;fill:#000000;stroke:none;}
          text{text-anchor:middle;}

          <svg viewBox="0 0 200 200" width="200" height="200">
          <g id="bg">
          <g>
          <rect x="41.4" y="40.189" width="117.199" height="120.236" transform="matrix(0.951057,-0.309017,0.309017,0.951057,-26.102184,35.811068)" fill="rgb(28,27,27)"/>
          <rect x="41.4" y="38.233" width="117.199" height="120.236" transform="matrix(0.961262,-0.275637,0.275637,0.961262,-23.235267,31.373671)" fill="rgb(57,57,57)"/>
          <rect x="41.4" y="38.233" width="117.199" height="120.236" transform="matrix(0.978148,-0.207912,0.207912,0.978148,-18.262998,22.940365)" fill="rgb(81,80,80)"/>
          <rect x="41.4" y="36.276" width="117.199" height="120.236" transform="matrix(0.984808,-0.173648,0.173648,0.984808,-15.219469,18.829264)" fill="rgb(149,149,149)"/>
          <rect x="41.4" y="34.32" width="117.199" height="120.236" transform="matrix(0.990268,-0.139173,0.139173,0.990268,-12.170036,14.836374)" fill="rgb(208,208,208)"/>
          <rect x="41.4" y="32.364" width="117.199" height="120.236" transform="matrix(0.994522,-0.104528,0.104528,0.994522,-9.11916,10.959471)" fill="rgb(255,255,255)"/>
          </g>
          <rect x="36.838" y="32.603" width="117.199" height="32.94" transform="matrix(0.994522,-0.104528,0.104528,0.994522,-4.606717,10.24477)" fill="rgb(255,63,56)"/>
          </g>

          </svg>

          <svg class="svg" viewBox="0 0 200 200" width="200" height="200">
          <use xlink:href="#bg" />
          <g transform="matrix(0.994522,-0.104528,0.104528,0.994522,95,57)">
          <text class="month" >NOV</text>
          <text class="day" x="0" y="75">16</text>
          </g>
          </svg>

          <svg class="svg" viewBox="0 0 200 200" width="200" height="200">
          <use xlink:href="#bg" />
          <g transform="matrix(0.994522,-0.104528,0.104528,0.994522,95,57)">
          <text class="month" >NOV</text>
          <text class="day" x="0" y="75">16</text>
          </g>
          </svg>





          let svgs = document.querySelectorAll(".svg")


          function changeDate(svg,month,day){
          svg.querySelectorAll("text")[0].textContent = month;
          svg.querySelectorAll("text")[1].textContent = day;
          }

          changeDate(svgs[0],"DEC","25");
          changeDate(svgs[1],"JAN","6");

          .month{font-family:'Open Sans';font-weight:400;font-size:20px;font-style:normal;word-spacing:5;fill:white;stroke:none;}
          .day{font-family:'Open Sans';font-weight:400;font-size:70px;font-style:normal;fill:#000000;stroke:none;}
          text{text-anchor:middle;}

          <svg viewBox="0 0 200 200" width="200" height="200">
          <g id="bg">
          <g>
          <rect x="41.4" y="40.189" width="117.199" height="120.236" transform="matrix(0.951057,-0.309017,0.309017,0.951057,-26.102184,35.811068)" fill="rgb(28,27,27)"/>
          <rect x="41.4" y="38.233" width="117.199" height="120.236" transform="matrix(0.961262,-0.275637,0.275637,0.961262,-23.235267,31.373671)" fill="rgb(57,57,57)"/>
          <rect x="41.4" y="38.233" width="117.199" height="120.236" transform="matrix(0.978148,-0.207912,0.207912,0.978148,-18.262998,22.940365)" fill="rgb(81,80,80)"/>
          <rect x="41.4" y="36.276" width="117.199" height="120.236" transform="matrix(0.984808,-0.173648,0.173648,0.984808,-15.219469,18.829264)" fill="rgb(149,149,149)"/>
          <rect x="41.4" y="34.32" width="117.199" height="120.236" transform="matrix(0.990268,-0.139173,0.139173,0.990268,-12.170036,14.836374)" fill="rgb(208,208,208)"/>
          <rect x="41.4" y="32.364" width="117.199" height="120.236" transform="matrix(0.994522,-0.104528,0.104528,0.994522,-9.11916,10.959471)" fill="rgb(255,255,255)"/>
          </g>
          <rect x="36.838" y="32.603" width="117.199" height="32.94" transform="matrix(0.994522,-0.104528,0.104528,0.994522,-4.606717,10.24477)" fill="rgb(255,63,56)"/>
          </g>

          </svg>

          <svg class="svg" viewBox="0 0 200 200" width="200" height="200">
          <use xlink:href="#bg" />
          <g transform="matrix(0.994522,-0.104528,0.104528,0.994522,95,57)">
          <text class="month" >NOV</text>
          <text class="day" x="0" y="75">16</text>
          </g>
          </svg>

          <svg class="svg" viewBox="0 0 200 200" width="200" height="200">
          <use xlink:href="#bg" />
          <g transform="matrix(0.994522,-0.104528,0.104528,0.994522,95,57)">
          <text class="month" >NOV</text>
          <text class="day" x="0" y="75">16</text>
          </g>
          </svg>






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 17 at 16:47

























          answered Nov 17 at 15:59









          enxaneta

          3,7571412




          3,7571412








          • 1




            Thank you! That is exactly what I wanted! You helped me so much!
            – Svenwas3f
            Nov 17 at 16:56














          • 1




            Thank you! That is exactly what I wanted! You helped me so much!
            – Svenwas3f
            Nov 17 at 16:56








          1




          1




          Thank you! That is exactly what I wanted! You helped me so much!
          – Svenwas3f
          Nov 17 at 16:56




          Thank you! That is exactly what I wanted! You helped me so much!
          – Svenwas3f
          Nov 17 at 16:56












          up vote
          0
          down vote













          this code worked for me, use your desired function (date or time) instead of New Text.



          var list = document.getElementsByTagName("g")[0]; 
          var textNode = list.getElementsByTagName("text")[0].children[0];
          textNode.textContent = "New Text";





          share|improve this answer





















          • So but by a table I may have on the first date no bookable time but in the next week it is possible so that with the [0] wouldn't work very well
            – Svenwas3f
            Nov 17 at 15:31















          up vote
          0
          down vote













          this code worked for me, use your desired function (date or time) instead of New Text.



          var list = document.getElementsByTagName("g")[0]; 
          var textNode = list.getElementsByTagName("text")[0].children[0];
          textNode.textContent = "New Text";





          share|improve this answer





















          • So but by a table I may have on the first date no bookable time but in the next week it is possible so that with the [0] wouldn't work very well
            – Svenwas3f
            Nov 17 at 15:31













          up vote
          0
          down vote










          up vote
          0
          down vote









          this code worked for me, use your desired function (date or time) instead of New Text.



          var list = document.getElementsByTagName("g")[0]; 
          var textNode = list.getElementsByTagName("text")[0].children[0];
          textNode.textContent = "New Text";





          share|improve this answer












          this code worked for me, use your desired function (date or time) instead of New Text.



          var list = document.getElementsByTagName("g")[0]; 
          var textNode = list.getElementsByTagName("text")[0].children[0];
          textNode.textContent = "New Text";






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 17 at 15:09









          Mohammad Moin

          508




          508












          • So but by a table I may have on the first date no bookable time but in the next week it is possible so that with the [0] wouldn't work very well
            – Svenwas3f
            Nov 17 at 15:31


















          • So but by a table I may have on the first date no bookable time but in the next week it is possible so that with the [0] wouldn't work very well
            – Svenwas3f
            Nov 17 at 15:31
















          So but by a table I may have on the first date no bookable time but in the next week it is possible so that with the [0] wouldn't work very well
          – Svenwas3f
          Nov 17 at 15:31




          So but by a table I may have on the first date no bookable time but in the next week it is possible so that with the [0] wouldn't work very well
          – Svenwas3f
          Nov 17 at 15:31


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53352375%2fchange-text-of-an-svg%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          RAC Tourist Trophy