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

Asked

Viewed 104 times

1

inserir a descrição da imagem aquiI tried to use getElementeById but it’s not working.

HTML Quiz

        <div class="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", "Garotinho", "Lula"], ... ]

EDIT: Gone Now in the log console takes a look, it seems that the error is this: Uncaught Typeerror: Cannot read Property '1' of Undefined in the array index.

  • 2

    Does it make a mistake? What gives console.log(array) ? I suggest you create a Minimum, Complete and Verifiable Problem Example to make it easier for the community to indicate the problem and its solution.

  • simply looks nothing, nor gives error in the console

  • You already answered the first question I asked. Now we must respond to the second and follow the advice I gave to set an example that shows the problem.

  • At what time/action(ie when) is to change these values ?

  • @luizricardo6n I tried to put as one of the first actions, but still appearing nothing on the button, I think in this case the position does not matter

  • Where was the index and of what value ?

  • no value, I used it to pick the position of my vector

Show 2 more comments

1 answer

1


Are you trying to change the value of span and the text of this tag is contained in innerHTML. To function normally change from value for 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’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.