0

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.

Demo at jsbin

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?

Community
  • 1
  • 1
doschni
  • 43
  • 8

1 Answers1

2

You need to trigger input event programmatically using .trigger()

  $('.clearable').trigger('input');

DEMO

Satpal
  • 132,252
  • 13
  • 159
  • 168