To enhance your input field, consider adding a type="search"
Although the support is good, please note that it won't function in IE versions less than 10.
For Older Browsers
If you require support for IE9, here are some alternatives:
Option 1: Using a standard <input type="text">
with additional HTML elements:
// JavaScript code to implement clearable text inputs
$(".clearable").each(function() {
const $inp = $(this).find("input:text"),
$cle = $(this).find(".clearable__clear");
$inp.on("input", function(){
$cle.toggle(!!this.value);
});
$cle.on("touchstart click", function(e) {
e.preventDefault();
$inp.val("").trigger("input");
});
});
/* CSS style for clearable text inputs */
.clearable{
position: relative;
display: inline-block;
}
.clearable input[type=text]{
padding-right: 24px;
width: 100%;
box-sizing: border-box;
}
.clearable__clear{
display: none;
position: absolute;
right:0; top:0;
padding: 0 8px;
font-style: normal;
font-size: 1.2em;
user-select: none;
cursor: pointer;
}
.clearable input::-ms-clear { /* Remove default 'X' icon from IE */
display: none;
}
<span class="clearable">
<input type="text" name="" value="" placeholder="">
<i class="clearable__clear">×</i>
</span>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
Option 2: Utilizing only <input class="clearable" type="text">
without additional elements
To implement this option, apply the class="clearable"
to your input and customize its background image accordingly:
// JavaScript code for implementing clearable text inputs
function tog(v){return v ? "addClass" : "removeClass";}
$(document).on("input", ".clearable", function(){
$(this)[tog(this.value)]("x");
}).on("mousemove", ".x", function( e ){
$(this)[tog(this.offsetWidth-18 < e.clientX-this.getBoundingClientRect().left)]("onX");
}).on("touchstart click", ".onX", function( ev ){
ev.preventDefault();
$(this).removeClass("x onX").val("").change();
});
// $('.clearable').trigger("input");
// Uncomment the line above if you pre-fill values from local storage or server
/* CSS styles for clearable text inputs */
.clearable{
background: #fff url(https://i.sstatic.net/mJotv.gif) no-repeat right -10px center;
border: 1px solid #999;
padding: 3px 18px 3px 4px; /* Make sure right padding (18) matches with jQuery setting! */
border-radius: 3px;
transition: background 0.4s;
}
.clearable.x { background-position: right 5px center; } /* Show icon using jQuery */
.clearable.onX{ cursor: pointer; } /* Provide hover cursor effect via jQuery */
.clearable::-ms-clear {display: none; width:0; height:0;} /* Remove default 'X' icon in IE */
<input class="clearable" type="text" name="" value="" placeholder="" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
The key approach here involves applying some right padding (e.g., 18px) to the input
, moving the background-image out of sight to the right (e.g., right -10px center
). This padding ensures that text does not hide under the icon when visible. Using jQuery, the class "x"
will be added if there's content in the input, showing the clear icon.
To further refine functionality, utilize jQuery to target inputs with class x
, determine during mousemove
whether the mouse is inside the 18px "x" region, add the class onX
accordingly. Clicking on the onX
class removes all classes, resets the input value, and hides the icon.
Gif dimensions: 7x7 pixels
Base64 string of the GIF:
data:image/gif;base64,R0lGODlhBwAHAIAAAP///5KSkiH5BAAAAAAALAAAAAAHAAcAAAIMTICmsGrIXnLxuDMLADs=