Menu
Coddy logo textTech

Hide the Toast Message

Part of the JavaScript in Action section of Coddy's HTML journey — lesson 12 of 27.

challenge icon

Challenge

Easy

Hide the Toast After 3 Seconds.

We don’t want the toast message to stay forever, so let’s hide it after a short delay:

  1. Use setTimeout() to create a 3-second delay.
  2. Inside the timeout function, add the "hidden" class to the toast element.
  3. Also remove the "show" class from the toast element so it disappears smoothly.

Try it yourself

<!DOCTYPE html>
<html>
<head>
  <title>Form with Toast</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <div class="container">
    <h2>Subscribe to our Newsletter</h2>
    <form id="myForm">
      <input type="text" id="name" placeholder="Enter your name">
      <button type="submit">Submit</button>
    </form>
  </div>

  <!-- Toast -->
  <div id="toast" class="toast hidden">Form submitted successfully!</div>

  <script src="script.js"></script>
</body>
</html>

All lessons in JavaScript in Action