I've found a jQuery script here at Clear icon inside input text which brings a clear-icon inside an input field. It fits perfect in my website. But it's not working, when the input value is prefilled.
I tried several onchange()
, change()
in additon to mousemove
and also replaced in the script itself and the following in html:
<body onLoad="tog(v);">
JS
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('click', '.onX', function(){
$(this).removeClass('x onX').val('');
});
But it did'nt help. Do you have any solution?