Hide Datalist on Hover
March 15, 2025 ยท AI Conversations
### **Approach**
– Hide the “ by default.
– Show it when clicking the associated “ or hovering over an arrow dropdown.
### **Example:**
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Datalist Dropdown</title>
<style>
.dropdown-wrapper {
position: relative;
display: inline-block;
}
.dropdown-arrow {
cursor: pointer;
padding: 5px;
background: lightgray;
border: 1px solid #ccc;
display: inline-block;
width: 20px;
text-align: center;
}
/* Hide datalist by default */
#myDatalist {
display: none;
position: absolute;
background: white;
border: 1px solid #ccc;
width: 100%;
max-height: 150px;
overflow-y: auto;
}
/* Show datalist when input is focused or arrow is hovered */
.dropdown-wrapper input:focus + #myDatalist,
.dropdown-wrapper:hover #myDatalist {
display: block;
}
</style>
</head>
<body>
<div class="dropdown-wrapper">
<input type="text" id="dropdownInput" placeholder="Select an option">
<span class="dropdown-arrow">▾</span> <!-- Downward arrow -->
<div id="myDatalist">
<div onclick="selectOption('Option 1')">Option 1</div>
<div onclick="selectOption('Option 2')">Option 2</div>
<div onclick="selectOption('Option 3')">Option 3</div>
</div>
</div>
<script>
function selectOption(value) {
document.getElementById('dropdownInput').value = value;
document.getElementById('myDatalist').style.display = 'none';
}
// Toggle dropdown when clicking the arrow
document.querySelector('.dropdown-arrow').addEventListener('click', function() {
let datalist = document.getElementById('myDatalist');
datalist.style.display = (datalist.style.display === 'block') ? 'none' : 'block';
});
// Hide dropdown when clicking outside
document.addEventListener('click', function(event) {
let wrapper = document.querySelector('.dropdown-wrapper');
let datalist = document.getElementById('myDatalist');
if (!wrapper.contains(event.target)) {
datalist.style.display = 'none';
}
});
</script>
</body>
</html>
### **How it Works**
1. The “ is initially hidden.
2. Clicking the input field or hovering over the dropdown arrow shows the options.
3. Clicking an option selects it and hides the dropdown.
4. Clicking outside the dropdown hides it.
This solution ensures the “ remains hidden until the user interacts with it properly. ๐
—
### **Solution:**
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hide Arrow on Focus</title>
<style>
.dropdown-wrapper {
position: relative;
display: inline-block;
}
.dropdown-arrow {
cursor: pointer;
padding: 5px;
background: lightgray;
border: 1px solid #ccc;
display: inline-block;
width: 20px;
text-align: center;
position: absolute;
right: 5px;
top: 5px;
}
/* Hide arrow when input is focused */
.dropdown-wrapper input:focus + .dropdown-arrow {
display: none;
}
/* Optional styling for input */
.dropdown-wrapper input {
padding-right: 30px; /* Space for arrow */
width: 200px;
padding: 8px;
border: 1px solid #ccc;
}
</style>
</head>
<body>
<div class="dropdown-wrapper">
<input type="text" id="dropdownInput" placeholder="Select an option">
<span class="dropdown-arrow">▾</span> <!-- Downward arrow -->
</div>
</body>
</html>
—
### **How It Works**
1. The `.dropdown-arrow` is **visible** by default.
2. When the “ is focused, the CSS rule `input:focus + .dropdown-arrow` **hides** the arrow.
3. The arrow remains hidden as long as the input is in focus.
4. Once the input loses focus, the arrow reappears.
This method ensures that when the user interacts with the input, the arrow disappears, creating a cleaner UI experience. ๐
—
### **Updated Code with Arrow Hidden on Focus**
<style>
/* Style the wrapper to contain input and pseudo dropdown arrow */
.product-input {
position: relative;
display: inline-block;
width: 100%;
}
/* Hide default dropdown arrow for datalist */
.product-input input::-webkit-calendar-picker-indicator {
display: none !important;
-webkit-appearance: none;
}
/* Custom dropdown arrow */
.product-input::after {
content: "โผ"; /* Downward arrow */
position: absolute;
right: 10px;
top: 50%;
transform: translateY(-50%);
font-size: 14px;
color: #555;
cursor: pointer;
pointer-events: none;
}
/* Hide arrow when input is focused */
.product-input input:focus + .product-input::after {
display: none;
}
/* Optional input styling */
.product-input input {
width: 100%;
padding: 8px;
font-size: 12px;
padding-right: 30px; /* Space for arrow */
border: 1px solid #ccc;
border-radius: 4px;
}
</style>
<div class="product-input">
<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">
</div>
<datalist id="product_code-<?php echo $product->get_id(); ?>">
<?php
if(is_array($p_sku) && count($p_sku) > 0){
foreach($p_sku as $data){
?>
<option value="<?php echo $data; ?>"></option>
<?php
}
}
?>
</datalist>
—
### **How It Works**
1. The `.product-input` wrapper **contains the input and a custom arrow (`โผ`)**.
2. The `::after` pseudo-element **acts as a fake dropdown arrow**.
3. The arrow **disappears** when the input is focused (`input:focus + .product-input::after`).
4. The “ still works normally.
5. The default browser arrow is removed using `::-webkit-calendar-picker-indicator`.
This ensures a clean UI where the dropdown arrow is hidden on focus but still functions as expected. ๐
—
### **Solution 1: Basic CSS Loader**
This loader is always hidden initially and appears when needed.
#### **CSS + HTML:**
<style>
.loader {
display: none; /* Hidden by default */
width: 30px;
height: 30px;
border: 3px solid #f3f3f3;
border-top: 3px solid #3498db;
border-radius: 50%;
animation: spin 1s linear infinite;
position: absolute;
right: 10px;
top: 50%;
transform: translateY(-50%);
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.product-input {
position: relative;
display: inline-block;
width: 100%;
}
/* Hide arrow when input is focused */
.product-input input:focus + .loader {
display: block;
}
.product-input input {
width: 100%;
padding: 8px;
font-size: 12px;
padding-right: 40px; /* Space for loader */
border: 1px solid #ccc;
border-radius: 4px;
}
</style>
<div class="product-input">
<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">
<div class="loader"></div> <!-- Loader -->
</div>
—
### **Solution 2: JavaScript Loader**
This method **shows the loader when the user selects a product and hides it when the selection is done**.
#### **JavaScript-Based Loader**
<style>
.loader {
display: none;
width: 30px;
height: 30px;
border: 3px solid #f3f3f3;
border-top: 3px solid #3498db;
border-radius: 50%;
animation: spin 1s linear infinite;
position: absolute;
right: 10px;
top: 50%;
transform: translateY(-50%);
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.product-input {
position: relative;
display: inline-block;
width: 100%;
}
.product-input input {
width: 100%;
padding: 8px;
font-size: 12px;
padding-right: 40px; /* Space for loader */
border: 1px solid #ccc;
border-radius: 4px;
}
</style>
<div class="product-input">
<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">
<div class="loader"></div> <!-- Loader -->
</div>
<script>
document.querySelector('.product_code').addEventListener('input', function() {
let loader = document.querySelector('.loader');
loader.style.display = 'block';
setTimeout(() => {
loader.style.display = 'none'; // Hide loader after 1.5 seconds (simulating data load)
}, 1500);
});
</script>
—
### **How It Works**
1. The **CSS loader** is initially hidden.
2. When the user types or selects a product code, the loader **appears**.
3. After a delay (simulating data fetching), the loader **hides automatically**.
This ensures a smooth user experience when handling product selections! ๐
—
### **Basic jQuery AJAX with Loader**
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AJAX Loader Example</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
/* Loader Style */
.loader {
display: none; /* Initially hidden */
width: 30px;
height: 30px;
border: 3px solid #f3f3f3;
border-top: 3px solid #3498db;
border-radius: 50%;
animation: spin 1s linear infinite;
position: absolute;
right: 10px;
top: 50%;
transform: translateY(-50%);
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.product-input {
position: relative;
display: inline-block;
width: 100%;
}
.product-input input {
width: 100%;
padding: 8px;
font-size: 12px;
padding-right: 40px; /* Space for loader */
border: 1px solid #ccc;
border-radius: 4px;
}
</style>
</head>
<body>
<div class="product-input">
<input type="text" class="product_code" name="product_code" placeholder="Enter or select Product Code">
<div class="loader"></div> <!-- Loader -->
</div>
<script>
$(document).ready(function () {
$('.product_code').on('input', function () {
let productCode = $(this).val();
let loader = $('.loader');
$.ajax({
url: "fetch_product.php", // Replace with your endpoint
type: "POST",
data: { product_code: productCode },
beforeSend: function () {
loader.show(); // Show loader before sending request
},
success: function (response) {
console.log("Success:", response);
// Process response here
},
error: function (xhr, status, error) {
console.error("Error:", error);
},
complete: function () {
loader.hide(); // Hide loader when request is complete
}
});
});
});
</script>
</body>
</html>
—
### **How It Works**
1. The `.loader` is **hidden by default**.
2. When the user **types in the input field**, an AJAX request is sent.
3. The `beforeSend` function **shows the loader** before making the request.
4. If the request succeeds (`success`) or fails (`error`), the `complete` function **hides the loader**.
This ensures that the user sees a loading animation while waiting for the response! ๐