-2
<!doctype html>
<html>
<body>

<form>
    <button onclick="myFunction()"> Click Me</button>
    <br>
    <hr>
    Words:<br>
    <input type= "text" id= "randomNumber">
</form>

<script>
        function myFunction(){
            var randomWords = ['Alpha','Beta','Gamma','Epsilon','Kappa','Omega'];
            var callNumber = Math.floor(Math.random()*6);
            var randomWord = randomWords[callNumber]
            document.getElementById('randomNumber').value=randomWord

}

</script>

</body>
</html>

'If you can then please remove the input box and print the words separetely' the result blinks in the box and then dissappears.

1 Answers1

0

The <button> is submitting the form. Move the button outside the form, prevent the form from submitting, use a different trigger element than <button>, etc. The issue is that the form is submitted (i.e. the page is reloaded) as soon as the button is clicked and the function is ran.

function myFunction(e){
  var randomWords = ['Alpha','Beta','Gamma','Epsilon','Kappa','Omega'];
  var callNumber = Math.floor(Math.random()*6);
  var randomWord = randomWords[callNumber]
  document.getElementById('randomNumber').value=randomWord
}
<button onclick="myFunction()"> Click Me</button>
<form>
    <br>
    <hr>
    Words:<br>
    <input type= "text" id= "randomNumber">
</form>
Xhynk
  • 13,513
  • 8
  • 32
  • 69