HTML Color Picker Input

User
html color picker but don't hide input box
Assistant
You can create an HTML color picker input while ensuring the input box remains visible by using the “ element with `type="color"`. To ensure the input box (for manual text input of hex codes) is also present, you can add a separate “ for the text field and synchronize it with the color picker. Here's how you can do it:

### Example Code:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Color Picker with Input Box</title>
  <style>
    .color-picker-container {
      display: flex;
      align-items: center;
      gap: 10px;
    }

    .color-input {
      padding: 5px;
      font-size: 1rem;
    }
  </style>
</head>
<body>
  <div class="color-picker-container">
    <!-- Color Picker -->
    <input type="color" id="colorPicker" class="color-input">

    <!-- Hex Input Box -->
    <input type="text" id="colorCode" class="color-input" placeholder="#000000">
  </div>

  <script>
    const colorPicker = document.getElementById('colorPicker');
    const colorCode = document.getElementById('colorCode');

    // Update text input when color picker changes
    colorPicker.addEventListener('input', (event) => {
      colorCode.value = event.target.value;
    });

    // Update color picker when text input changes
    colorCode.addEventListener('input', (event) => {
      const value = event.target.value;
      if (/^#[0-9A-Fa-f]{6}$/.test(value)) { // Validate hex color code
        colorPicker.value = value;
      }
    });
  </script>
</body>
</html>

### Features:
1. **Color Picker Input**: Lets you pick a color visually.
2. **Text Input**: Allows manual input of a hex color code (e.g., `#FF5733`).
3. **Synchronization**: Changes in one input reflect in the other.

### Notes:
– The `type="color"` input provides the browser's built-in color picker.
– The JavaScript ensures the two inputs are synchronized.
– Input validation ensures that only valid hex codes are accepted in the text box.

User
adjust above
Assistant
Here’s an adjusted version of your code that includes a visible text input field synchronized with the color picker. This ensures users can either pick a color visually or manually enter a hex code.

### Adjusted Code:

<div class="mb-3">
  <label for="primaryColor" class="form-label">Primary Color</label>
  <div class="d-flex align-items-center gap-2">
    <!-- Color Picker -->
    <input 
      type="color" 
      class="form-control form-control-color" 
      id="primaryColor" 
      name="primaryColor" 
      value="{{ old('primaryColor', '#000000') }}"
    >

    <!-- Text Input for Hex Code -->
    <input 
      type="text" 
      class="form-control" 
      id="primaryColorText" 
      name="primaryColorText" 
      value="{{ old('primaryColor', '#000000') }}" 
      placeholder="#000000"
    >
  </div>
</div>

<script>
  const colorPicker = document.getElementById('primaryColor');
  const colorText = document.getElementById('primaryColorText');

  // Synchronize text input when color picker changes
  colorPicker.addEventListener('input', (event) => {
    colorText.value = event.target.value;
  });

  // Synchronize color picker when text input changes
  colorText.addEventListener('input', (event) => {
    const value = event.target.value;
    if (/^#[0-9A-Fa-f]{6}$/.test(value)) { // Validate hex color code
      colorPicker.value = value;
    }
  });
</script>

### Changes and Additions:
1. **Added a Text Input**:
– `id="primaryColorText"`
– To allow users to manually input a hex color code.

2. **Default Value**:
– Ensured `old('primaryColor', '#000000')` provides a fallback default value for both inputs.

3. **JavaScript Synchronization**:
– Updates the color picker when the text input changes and vice versa.
– Includes validation for proper hex color codes.

4. **Bootstrap Styling**:
– Used `form-control` class for consistency with Bootstrap forms.

5. **Accessibility and UX**:
– Label added for `primaryColor` for better form accessibility.
– Both inputs are visually aligned with `d-flex` and `gap-2` classes.

Leave a Comment

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