Set an onclick handler for the button and then have it trigger a javascript function - note the use of the alert - just to show the functioning of the function:
function promptMe(){
var userAdjective = prompt("Please provide an Adjective");
alert (userAdjective);
}
<button id="bgnBtn" onclick="promptMe()">Start Game</button>
Answer from gavgrif on Stack OverflowSet an onclick handler for the button and then have it trigger a javascript function - note the use of the alert - just to show the functioning of the function:
function promptMe(){
var userAdjective = prompt("Please provide an Adjective");
alert (userAdjective);
}
<button id="bgnBtn" onclick="promptMe()">Start Game</button>
I would recommend attaching an event listener to the button in your JS code.
document.querySelector('#bgnBtn').addEventListener('click', function() {
var userAdjective = prompt("Please provide an Adjective");
alert (userAdjective);
}
This example uses an anonymous function for handling the click event. You could also do the following:
document.querySelector('#bgnBtn').addEventListener('click', promptMe);
function promptMe() {
var userAdjective = prompt("Please provide an Adjective");
alert (userAdjective);
}
Videos
You could use jQuery and do a 3 button dialog(). Check out this working jsFiddle demo:
$("#dialog").dialog({
autoOpen: true,
buttons: {
Yes: function() {
alert("Yes!");
$(this).dialog("close");
},
No: function() {
alert("No!");
$(this).dialog("close");
},
Maybe: function() {
alert("Maybe!");
$(this).dialog("close");
}
},
width: "400px"
});
This can't be achieved natively as .prompt(). This functionality requires more advanced JS. Here are some libs to mess with:
- jQuery UI (dialog)
- jQuery Tools - Overlay
Could be more adequate ones out there. Been a while using such stuff