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>
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
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
function myFunction(){
var x = document.getElementById("nom").value;
affichepanier(x);
}
function affichepanier(val)
{
var variableOfFunction1 = val;
document.getElementById("deux").innerHTML =variableOfFunction1 ;
}