Advertisement
Guest User

Html - Code Button

a guest
Mar 3rd, 2022
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 0.65 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <a id="myButton" href="#">
  4.     click here to get random stuff
  5. </a>
  6.  
  7. <div id="myRandomDiv">
  8. </div>
  9.  
  10. <script type="text/javascript" charset="utf-8">
  11.     var randomStrings = [
  12.         "hello 1",
  13.         "hello 2",
  14.         "hello 3",
  15.         "hello 4",
  16.         "hello 5",
  17.     ];
  18.  
  19.  
  20.  
  21.     var randomDiv = document.getElementById("myRandomDiv");
  22.  
  23.     document.getElementById("myButton").addEventListener("click", function() {
  24.           randomIndex = Math.ceil((Math.random()*randomStrings.length-1));
  25.           newText = randomStrings[randomIndex];
  26.           randomDiv.innerHTML = newText;
  27.     });
  28. </script>
  29.  
  30. </html>
  31.  
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement