Show dropdown arrow always

User
Assistant
The “ element does not show a dropdown arrow by default in most browsers. However, you can use a combination of CSS and JavaScript to force the dropdown to be visible and make it behave like a normal “ element.

### Solution:
1. **Use CSS to add a dropdown arrow manually.**
2. **Use JavaScript to trigger the dropdown on focus.**

#### โœ… **Updated Code**

<style>
/* Force the appearance of a dropdown */
.product_code {
  appearance: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  background-image: url('data:image/svg+xml;charset=US-ASCII,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 4 5"><path fill="black" d="M2 0L0 2h4z"/></svg>'); 
  background-repeat: no-repeat;
  background-position: right 8px center;
  background-size: 10px;
  padding-right: 20px; /* Space for arrow */
  cursor: pointer;
}

/* Optional: Style the dropdown options */
datalist option {
  font-size: 12px;
}
</style>

<input list="product_code-<?php echo $product->get_id(); ?>" class="product_code" name="product_code" value="" 
       style="font-size: 12px;" data-pid="<?php echo $product->get_id(); ?>" 
       placeholder="Enter or select the Product Code">
<datalist id="product_code-<?php echo $product->get_id(); ?>">
    <?php 
    if (is_array($p_sku) && count($p_sku) > 0) {
        foreach ($p_sku as $data) {
            echo "<option value="$data"></option>";
        }
    }
    ?>
</datalist>

<script>
document.addEventListener("DOMContentLoaded", function() {
    const inputs = document.querySelectorAll(".product_code");

    inputs.forEach(input => {
        input.addEventListener("focus", function() {
            this.setAttribute("list", this.getAttribute("data-pid"));
        });

        input.addEventListener("click", function() {
            this.setAttribute("list", this.getAttribute("data-pid"));
        });
    });
});
</script>

### ๐Ÿ”น **How It Works**
1. **CSS**:
– Uses `appearance: none;` to remove default styling.
– Adds a manual dropdown arrow using a base64 SVG image as `background-image`.
– Increases `padding-right` to prevent text overlap with the arrow.

2. **JavaScript**:
– Forces the “ to open when the input field is focused or clicked.
– Ensures that the correct `list` attribute is set dynamically.

This will make the datalist behave more like a traditional dropdown. ๐Ÿš€ Let me know if you need further refinements!

Leave a Comment

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