Home »
JavaScript Examples
JavaScript | Input value from the user using prompt
JavaScript Input value from the user using prompt: Here, we will read values from the user using prompt and print the input values.
Submitted by Pankaj Singh, on October 22, 2018
Example 1) Input name and print
Code (JS & HTML):
<!DOCTYPE html>
<HTML>
<HEAD>
<SCRIPT>
var name = prompt("Enter Your name:");
var msg = "Welcome "+name;
//alert(msg);
document.write(msg);
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>
Output
Example 2) Input two numbers and find their sum
Code (JS & HTML):
<!DOCTYPE html>
<HTML>
<HEAD>
<SCRIPT>
function Calculate(){
var a=parseInt(document.getElementById("txta").value);
var b=parseInt(document.getElementById("txtb").value);
var c=a+b;
document.getElementById("txtc").value=""+c;
}
</SCRIPT>
</HEAD>
<BODY>
<h2>The Sum Program</h2>
<hr />
<table>
<tr>
<td>
<label>Enter A:</label>
</td>
<td>
<input type="text" name="txta" id="txta" />
</td>
</tr>
<tr>
<td>
<label>Enter B:</label>
</td>
<td>
<input type="text" name="txtb" id="txtb" />
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type="button" value="Calculate" onclick="Calculate()" />
</td>
</tr>
<tr>
<td>
<label>Sum</label>
</td>
<td>
<input type="text" name="txtc" id="txtc" readonly />
</td>
</tr>
</table>
</BODY>
</HTML>
Output
JavaScript Examples »