When the user clicks on the button, the onclick event has been fired and the JavaScript message listed in the value of the attribute are executed.
Example
Your Name is ____. The city in which you live is ____.
<!DOCTYPE html>
<html>
<head>
<title>User Input</title>
</head>
<body>
<button id="name">Name</button>
<button id="city">City</button>
<p>
Your Name is <span id="outputName">____</span>. The city in which you live is <span id="outputCity">____</span>.
</p>
<script>
document.getElementById("name").onclick = function(){
var name = prompt("Enter your name");
document.getElementById("outputName").innerText = name;
}
document.getElementById("city").onclick = function(){
var city = prompt("Enter your city");
document.getElementById("outputCity").innerText = city;
}
</script>
</body>
</html>
Most Helpful This Week
Generate random password for form field on click using JavaScript
Highlight and get the details of table row on click using JavaScript
How to join string with number in JavaScript?
How to current URL, HOST and URL attributes using JavaScript or jQuery?
jQuery Show or Hide text on button click
How to scroll page to particular Div on click using jQuery?
jQuery show input text inside div after on click event
ES6 method of declaration multiple variables in one line
How to define Global Variable in JavaScript?
What's the difference between let and var?