How to call a variable within another function

Asked

Viewed 129 times

3

I’m trying to call the name variable within the forward functionQuadro. But it’s not working.

var pergunta = document.querySelectorAll(".pergunta");
var indiceAtual = 0;

function iniciar(){
    pergunta[0].style.display = "block";
}

function avancaPergunta(){ //pula de pergunta em pergunta
    var nome = document.getElementById("nome").value;    
    var mesNascimento = document.getElementById("mes-nascimento").value;
    var cor = document.getElementById("cor").value;
    pergunta[indiceAtual].style.display = "none";
    if (indiceAtual +1 >= pergunta.length){
        alert("você terminou o quiz");
        return;
    }

    pergunta[indiceAtual+1].style.display = "block";
    indiceAtual++; 

}

function avancaQuadro(){
    document.getElementById("resultado22").innerHTML = nome;

}

1 answer

2

The forward function is not receiving any parameters to pass. Place the name parameter inside the parentheses:

function avancaQuadro(nome){
    document.getElementById("resultado22").innerHTML = nome;

}

And when calling the function, you enter the parameter to be used:

avancaQuadro('um texto');

The variable nome that you created in function avancaPergunta() will only be accessed within the scope of this function. If you intend to use this same variable elsewhere, it needs to be declared outside the scope.

  • 1

    Roberto, I made this change.. I put the name variable outside the function, but it’s not rolling... I don’t know if the problem var name = Document.getElementById("name"). value; Function avancaPergunta(){ //pula of question in question question[indiceAtual].style.display = "None"; if (indiceAtual +1 >= question.length){ Alert("you finished the quiz"); Return; } question[indiceAtual+1].style.display = "block"; indiceAtual++; } Function advantageQuadro(){ Document.getElementById("resultado22"). innerHTML = name; }

  • Call the variable without the . value at the end and inside the forward functionTuadro, try setting the innerHTML with name.value; to see if it works then.

Browser other questions tagged

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