Starting a javascript prompt after a button is clicked

喜欢而已 提交于 2019-12-07 04:11:25

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>

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);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!