Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

148
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!