How to change the value of a button to the value of an array?

1

ItriedtousegetElementeByIdbutitisnotworking.

    

                    

HTMLQuiz

                         
<divclass="buttons"> <!--Creating four button element for four options-->
            <button id="btn0"><span id="choice0"></span></button> 
            <button id="btn1"><span id="choice1"></span></button>
            <button id="btn2"><span id="choice2"></span></button>
            <button id="btn3"><span id="choice3"></span></button>
        </div>

function quiz(){
    document.getElementById("choice0").value = array[index][1];
    document.getElementById("choice1").value = array[index][2];
    document.getElementById("choice2").value = array[index][3];
    document.getElementById("choice3").value = array[index][4];
    }

var array = [         ["Who discovered Brazil?", "Cabral", "Paes", "Little Boy", "Lula" ... ]

  

EDIT: I have now logged into the console log, it looks like the error is this: Uncaught TypeError: Can not read property '1' of undefined in array index .

    
asked by anonymous 15.10.2018 / 20:00

1 answer

0

You are trying to change the content value of span and the text of that tag is contained in innerHTML . To work normally change from value to innerHTML :

function quiz(){
    document.getElementById("choice0").innerHTML = array[index][1];
    document.getElementById("choice1").innerHTML = array[index][2];
    document.getElementById("choice2").innerHTML = array[index][3];
    document.getElementById("choice3").innerHTML = array[index][4];
}

I hope I have helped.

    
15.10.2018 / 21:32