Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

149
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda