Hide and Show Text Fields Based on Button Click

In this task, we will create a simple JavaScript application that allows users to hide and show text fields based on a button click. The application will consist of a button that, when clicked, toggles the visibility of one or more text fields. This functionality can be useful in forms where certain fields are only relevant based on user input.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Toggle Text Fields</title>
    <style>
        .hidden {
            display: none;
        }
    </style>
</head>
<body>
    <h1>Toggle Text Fields Example</h1>
    <button id="toggleButton">Show Text Fields</button>
    
    <div id="textFields" class="hidden">
        <label for="field1">Field 1:</label>
        <input type="text" id="field1" name="field1"><br><br>
        
        <label for="field2">Field 2:</label>
        <input type="text" id="field2" name="field2"><br><br>
    </div>

    <script>
        const toggleButton = document.getElementById('toggleButton');
        const textFields = document.getElementById('textFields');

        toggleButton.addEventListener('click', () => {
            if (textFields.classList.contains('hidden')) {
                textFields.classList.remove('hidden');
                toggleButton.textContent = 'Hide Text Fields';
            } else {
                textFields.classList.add('hidden');
                toggleButton.textContent = 'Show Text Fields';
            }
        });
    </script>
</body>
</html>

Code Explanation

In the provided code, we have created a simple HTML document that includes a button and two text fields. Here’s a breakdown of the key components:

  1. HTML Structure:
    • The document starts with a standard HTML structure, including a <head> section for metadata and styles, and a <body> section for the content.
    • A button with the ID toggleButton is created to control the visibility of the text fields.
    • <div> containing two text fields is initially hidden using the hidden class.
  2. CSS Styling:
    • The .hidden class is defined in the <style> section to set display: none;, which effectively hides any element with this class.
  3. JavaScript Functionality:
    • We select the button and the text fields using document.getElementById.
    • An event listener is added to the button that listens for click events. When the button is clicked, the following occurs:
      • The code checks if the textFields div has the hidden class.
      • If it does, the class is removed, making the text fields visible, and the button text is updated to “Hide Text Fields”.
      • If the hidden class is not present, it is added back, hiding the text fields again, and the button text changes to “Show Text Fields”.

This simple yet effective implementation allows users to interactively show or hide text fields, enhancing the user experience in forms.

Happy coding… 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *