Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

150
Visualizações
how to extract a javascript variable from a function

I want to retrieve the variable x from the function, but this code does not work who has an idea

thank you

function myFunction(){
  var x = document.getElementById("nom").value;
  //document.getElementById("demo").innerHTML =x ;

 return x;
}

response = myFunction();

document.getElementById("deux").innerHTML =response ;
<form class="form">
<label>Entre ton nom</label>
<input type="text" id="nom" name="Nom" placeholder="Nom" value="" class="form-control">
<input type="button" name="Envoyer" class="btn btn-default" value="Envoyer" onclick="myFunction();">
</form>

 <script src="test-envois.js"></script>
   <p id="deux"></p>
  <p id="demo"></p>
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

From what can I see in the code is that you want to add the response to the deux id when you click on the button The problem in that is that response executes the moment you load the page. You need to add that logic inside the function like this:

function myFunction() {
    var x = document.getElementById("nom").value;
    //document.getElementById("demo").innerHTML =x ;
    // let response = myFunction();

    document.getElementById("deux").innerHTML = x;
}

And now it executes when you click the button

about 4 years ago · Juan Pablo Isaza Relatório

0

If I understand right, you want whatever the user inputs in the form "nom" to be displayed in the paragraph tag "deux"?

if that’s the case you could just do:

function myFunction() {
var x = document.getElementById("nom").value;
document.getElementById("deux").innerHTML = x;
}

you can enclose all the code into one function, no need to separate it

about 4 years ago · Juan Pablo Isaza Relatório

0

function myFunction(){
  var x = document.getElementById("nom").value;
 affichepanier(x);
 
}

function affichepanier(val)
{
  var variableOfFunction1 = val;
  document.getElementById("deux").innerHTML =variableOfFunction1 ;
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda