DjangoDev DjangoDev - 4 days ago 7
Javascript Question

Getting the value of prompt box into another function

Please check out the code below. I want to get the value entered in the prompt box into function dis(). How can I do that?

<!DOCTYPE html>
<html>
<head>
<script>
function display()
{
var z=prompt("enter your name...");
if(z!=null)
{
document.getElementById("demo").innerHTML="thankyou"+z+"..";
document.getElementById("case").style.display='block';
}
else
document.getElementById("demo").innerHTML="thankyou";
}
function dis()
{

var a=document.getElementById("aaa").value;
alert("your mark is"+a);
}
</script>
</head>
<body>
<p id="demo">click on the button.....</p>
<button type="button" onclick="display()">submit</button>
<div id="case" style="display:none">
<input type="text" id="aaa" name="myText" onDblClick="dis()">enter your mark
</div>
</body>
</html>

Answer

If you want to directly pass value to dis() function then change your script to

function display() {
     var z = prompt("enter your name...");
     if (z != null) {
         document.getElementById("demo").innerHTML = "thankyou " + z + "..";
         document.getElementById("case").style.display = 'block';
         dis(z);
     }
     else
         document.getElementById("demo").innerHTML = "thankyou";
 }
 function dis(arg) {
     alert("your mark is" + arg);
 }
Comments