2

im trying to make it so when i press a button it asks me a question from window.prompt, i tried this but it didnt work

<!DOCTYPE html>
<html>
    <head>
        <script>
            var button = document.getElementById('btn')
            function test(){
                window.prompt("test")
            }
        </script>
    </head>
    <body>
        <button id="btn" onkeyup="myFunction()">test</button>
    </body>
</html>
Twisty
  • 35
  • 5

1 Answers1

1

You need to pass it the name of the function and use the right event. like this:

<!DOCTYPE html>
<html>
    <head>
        <script>
            function test(){
                window.prompt("test")
            }
        </script>
    </head>
    <body>
        <button id="btn" onclick="test()">test</button>
    </body>
</html>

I have changed the onkeyup to onclick, because you click the button. and gave it the test function which was the name you choose for the function.

Ran Marciano
  • 1,431
  • 5
  • 13
  • 30