Create a Fun Random Word Generator With HTML and JavaScript

by NightmareHDH in Circuits > Websites

2051 Views, 2 Favorites, 0 Comments

Create a Fun Random Word Generator With HTML and JavaScript

unnamed.png
Screenshot 2023-10-13 8.23.07 PM.png

In this Instructables article, I'll guide you through the process of creating a simple yet entertaining Random Word Generator using HTML and JavaScript. Whether you're a beginner or just looking for a fun project, this is a great way to dive into the basics of web development.

Supplies

Screenshot 2023-10-13 8.09.15 PM.png
  1. A text editor (e.g., Visual Studio Code, Sublime Text, or even Notepad)
  2. Web browser

OR

You can use W3Schools Tryit editor (I'll be using this)

Setting Up the HTML Structure

Screenshot 2023-10-13 8.14.56 PM.png

Begin by creating the HTML structure for your random word generator. You can do this by opening your preferred text editor and creating a new HTML file, naming it something like `random_word_generator.html`.

Here's a basic HTML template to get you started:


<!DOCTYPE html>

<html>

<head>

  <title>Random Word Generator</title>

</head>

<body>

  <h1>Random Word Generator</h1>

  <p>Your random word will appear here:</p>

  <button id="generateButton">Generate Word</button>

  <!-- JavaScript code goes here -->

</body>

</html>


This HTML template includes a title, a heading, a paragraph to display the random word, and a button that will trigger the word generation.

Adding JavaScript

Screenshot 2023-10-13 8.15.55 PM.png

Now, let's add the JavaScript code between the `<script>` tags in your HTML file. This code will generate random words from an array.

Here's a simple example using an array of words:

<script>

  // Define an array of words

  const words = ["apple", "banana", "cherry", "date", "elderberry", "fig", "grape"];


  // Get a reference to the button element

  const generateButton = document.getElementById("generateButton");


  // Function to generate a random word

  function generateRandomWord() {

    const randomIndex = Math.floor(Math.random() * words.length);

    const randomWord = words[randomIndex];

    document.querySelector("p").textContent = `Random Word: ${randomWord}`;

  }


  // Attach an event listener to the button

  generateButton.addEventListener("click", generateRandomWord);

</script>


This JavaScript code defines an array of words, creates a function to generate random words, and adds an event listener to the button. When the button is clicked, it will display a random word from the array.

Testing Your Random Word Generator

Screenshot 2023-10-13 8.23.07 PM.png

Save your HTML file and open it in a web browser and if you are using Tryit then Click Run. You should see your Random Word Generator with the "Generate Word" button. Click the button, and voilĂ , a random word will appear each time you click it!


Mine Look Different because I have add some CSS

Conclusion

Screenshot 2023-10-13 8.26.04 PM.png

Congratulations! You've created a basic Random Word Generator using HTML and JavaScript. This project serves as a fantastic starting point to understand how HTML and JavaScript can work together to create interactive web applications. You can enhance and customize this project by adding more words to the array, improving the user interface, or incorporating additional features. Have fun experimenting and learning more about web development...