Highlight the currently selected form field

Simple JavaScript example to highlight selected form fields with CSS for improved usability and accessibility in any HTML form.

Brief Introduction

Highlight the currently selected form field (javascript)
DESCRIPTION:
A simple example of an HTML form with input fields that automatically highlight when selected (focused). The code uses JavaScript to add and remove CSS classes that change the appearance of the active field, improving the usability and accessibility of the form.

ADDITIONAL NOTES:
The code is ready to use and easily integrable into any web page. The approach used is simple and does not require external libraries, ensuring maximum compatibility and lightness.

EXPLANATIONS:
- The JavaScript function adds the 'highlighted' class to the input field when it receives focus, changing its visual appearance.
- When the field loses focus (blur event), the 'highlighted' class is removed to revert to the original style.
- The CSS class 'highlighted' defines a colored border and a shadow to make the active field stand out.
- Events are assigned to all input fields of the form via a loop, making the code scalable to any number of fields.

Example Code: javascript

// Complete code to highlight the currently selected form field

// HTML + CSS + JavaScript integrated in a single file for ease of use

const formHTML = `
  <style>
    /* Base style for input fields */
    input {
      padding: 8px;
      margin: 6px 0;
      border: 2px solid #ccc;
      border-radius: 4px;
      font-size: 16px;
      transition: border-color 0.3s, box-shadow 0.3s;
      width: 100%;
      box-sizing: border-box;
    }

    /* Style applied to the active (focused) field */
    input.evidenziato {
      border-color: #007BFF; /* bright blue */
      box-shadow: 0 0 8px rgba(0, 123, 255, 0.6);
      outline: none; /* removes the browser's default border */
    }

    /* Form container for sizing and layout */
    .form-container {
      max-width: 400px;
      margin: 20px auto;
      font-family: Arial, sans-serif;
    }

    label {
      display: block;
      margin-top: 12px;
      font-weight: bold;
    }

    button {
      margin-top: 16px;
      padding: 10px 15px;
      font-size: 16px;
      background-color: #007BFF;
      color: white;
      border: none;
      border-radius: 4px;
      cursor: pointer;
      transition: background-color 0.3s;
    }

    button:hover {
      background-color: #0056b3;
    }
  </style>

  <div class="form-container">
    <form id="mioForm">
      <label for="nome">Name:</label>
      <input type="text" id="nome" name="nome" placeholder="Enter your name" required>

      <label for="email">Email:</label>
      <input type="email" id="email" name="email" placeholder="example@domain.com" required>

      <label for="password">Password:</label>
      <input type="password" id="password" name="password" placeholder="Enter your password" required>

      <button type="submit">Submit</button>
    </form>
  </div>
`;

// Insert the form into the page body
document.body.innerHTML = formHTML;

// Function to handle highlighting of the active field
function evidenziaCampi() {
  // Select all inputs inside the form
  const inputs = document.querySelectorAll('#mioForm input');

  // For each input add listeners for focus and blur
  inputs.forEach(input => {
    // When the field receives focus, add the 'evidenziato' class
    input.addEventListener('focus', () => {
      input.classList.add('evidenziato');
    });

    // When the field loses focus, remove the 'evidenziato' class
    input.addEventListener('blur', () => {
      input.classList.remove('evidenziato');
    });
  });
}

// Initialize the function after the page loads
window.addEventListener('DOMContentLoaded', evidenziaCampi);
×
×
×
Do you want to regenerate the answer?
×
Export chat
Choose export format
Generazione PDF in corso…
Allegati
×
You are about to close the chat and switch to the image generator. If you are not logged in, you will lose our chat. Do you confirm?
You are viewing a shared chat in temporary mode. It will not be saved.
×
Saved prompts
×
Private note
×
Label
×
Search all chats
×
Your insights
Analyzing…
×
AI Preferences
Assistant name 30
Answer length
AI model
Structured answers
Include examples
Use emojis
Guide me, don't solve Hints and questions instead of a ready answer
More settings →
×
Share this chat
Anyone opening this link can view the chat or add it to their profile as their own chat.
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
Shared chat
Someone shared a chat with you. Do you want to just view it or add it to your chats?
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
×

Saved messages

Loading...

×

Chat History

example-programs · CHAT HISTORY

Loading...

AI Preferences

×
  • BasicQuick and essential answers for study
  • MediumHigher quality for study and programming
  • AdvancedComplex reasoning and detailed analysis
Explain Steps

What do you need?

HTML: form structure with input fields (text, email, password)

CSS: definition of base style and highlighted style (focus)

JavaScript: handling of 'focus' and 'blur' events to add/remove CSS classes

Software configuration: no external dependencies, vanilla JavaScript code compatible with all modern browsers

Arduino: not applicable (no hardware involved)

Similar Topics