-2
    <button onclick="changefigure()">2011</button>
    <figure>
        <img id="Image" src="NT_Naplan_Reading_Results_2017.png"/>
        <figcaption id="caption"><em>Fig 2</em>Percent of children above national minimum standard in 
        reading in 2017 for Year 3, 5, 7 and 9 for Non-Indigenous and Indigenous children in the 
        Northern Territory. Data Source <a href="">NAPLAN results</a></figcaption>
    </figure>

    <script>
        function changefigure()
        {
            var x = document.getElementById("Image");
            var y = document.getElementById("caption")
            x.src = 'NT_Naplan_Reading_Results_2011.png'
            //y = "<em>Fig 1</em>Percent of children above national minimum standard in reading in 
            2017 for Year 3, 5, 7 and 9 for Non-Indigenous and Indigenous children in the Northern 
            Territory. Data Source <a href="">NAPLAN results</a>"
        }
    </script>

So I can't seem to be able to change the figcaption. I've tried using y.innerHTML and y.innertext but both of them dont seem to work. Any tips?

Felix F.
  • 3
  • 1
  • 1
  • What exactly is the problem you are having? Are you getting any errors in the console? Please show us the code you were using to set the figcaption in the JS so we can see what might have been causing the problem. – FluffyKitten Sep 02 '20 at 03:06
  • it should work. It could be you have syntax errors because you are building the new string the wrong way. Put multiple line string inside two ` characters. – lastr2d2 Sep 02 '20 at 03:06

1 Answers1

0

So i found out what's wrong with my code. Instead of using " use ' for it to for. E.G.

<script>
    function changefigure()
    {
        var x = document.getElementById("Image");
        var y = document.getElementById("caption")
        x.src = 'NT_Naplan_Reading_Results_2011.png'
        y.innerHTML = '<em>Fig 1</em>Percent of children above national minimum 
        standard in reading in 2017 for Year 3, 5, 7 and 9 for Non-Indigenous and 
        Indigenous children in the Northern Territory. Data Source <a href="">NAPLAN 
        results</a>'
    }
</script>
Felix F.
  • 3
  • 1
  • 1
  • In Javascript ES6, it is recommended to use [template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) if you're having multi-line strings or string interpolation. – Alfred Sep 02 '20 at 03:27