83

On chrome, the "search" event is fired on search inputs when user clicks the clear button.

Is there a way to capture the same event in javascript on Internet Explorer 10?

enter image description here

ghusse
  • 3,070
  • 2
  • 19
  • 29
  • 1
    Possible duplicate : http://stackoverflow.com/questions/2492722/is-there-any-events-available-for-the-reset-option-for-input-search – wakooka Jan 24 '13 at 10:00
  • Nope, this one is all about IE10. On webkit, I know how to handle this. – ghusse Jan 24 '13 at 10:02
  • There's no event fired in IE10 when clearing the field. – wakooka Jan 24 '13 at 10:06
  • Quick shot, haven't read the spec on search inputs in ages, but: can you watch the 'input' event instead? – natevw Apr 11 '13 at 22:11
  • 2
    Hmm, might still be worth a try but looks like IE also has a buggy 'input' event: http://help.dottoro.com/ljhxklln.php (fired only when text is added, not when removed!) – natevw Apr 11 '13 at 22:19
  • I created a test page where I added a callback to every single event on a text input in javascript : no event is fired except click events. – ghusse Apr 16 '13 at 09:23
  • 3
    @ghusse the 'input' event gets fired. – kzh Oct 30 '13 at 20:52

9 Answers9

72

The only solution I finally found:

// There are 2 events fired on input element when clicking on the clear button:
// mousedown and mouseup.
$("input").bind("mouseup", function(e){
  var $input = $(this),
      oldValue = $input.val();

  if (oldValue == "") return;

  // When this event is fired after clicking on the clear button
  // the value is not cleared yet. We have to wait for it.
  setTimeout(function(){
    var newValue = $input.val();

    if (newValue == ""){
      // Gotcha
      $input.trigger("cleared");
    }
  }, 1);
});
ghusse
  • 3,070
  • 2
  • 19
  • 29
  • 1
    Thanks Ghusse! If someone comes up with any other solution (without the setTimeout), let me know. – He Nrik Nov 06 '13 at 07:43
  • This works for ASP.NET / jQuery / IE11 as well. Instead of $("input").bind("mouseup", function(e){ you need to use $('#').mouseup(function (){ – Joe Schmoe Oct 22 '14 at 14:57
  • 3
    The solution by Lucent below works for me in IE11. His solution is to use the `oninput` event instead. When you press the X icon the `input` event fires. – iheartcsharp Dec 29 '15 at 15:11
  • shouldn't the if statement be `if (oldValue === "")`? – Dylan Czenski Sep 01 '16 at 20:36
  • While the solution by Lucent with the `input`-event works, this solution is superior in that it only fires when the element was cleared, while Lucent's also fires when the cursor enters the element, leaves the element, and also when data has been entered. – spheenik Feb 21 '17 at 09:18
  • If you are on lower versions for IE (<10), go ahead with this solution. I tried `oninput` on IE8 but doesn't work! – ScrapCode Jul 26 '17 at 09:27
  • At last found solution here and it is working for me in IE. Thank you. – Dhanik Lal Sahni Jun 23 '18 at 12:14
40

The oninput event fires with this.value set to an empty string. This solved the problem for me, since I want to execute the same action whether they clear the search box with the X or by backspacing. This works in IE 10 only.

Lucent
  • 1,296
  • 15
  • 22
  • 1
    this works for me too in IE10, don't think earlier versions on IE have a clear field button, so should only need to work for IE10 – andyface Sep 26 '14 at 10:58
  • 3
    Works in IE11 aswell! – AfroMogli Jul 21 '15 at 09:32
  • 1
    This works in Chrome and Firefox as well and also fires when the user right clicks mouse and cuts the value. To hook up use this: `inputEl.addEventListener('input', function(){ /* DoSomething */ })`. – Derek Oct 04 '18 at 14:52
32

Use input instead. It works with the same behaviour under all the browsers.

$(some-input).on("input", function() { 
    // update panel
});
user2441511
  • 10,816
  • 3
  • 18
  • 47
10

Why not

$("input").bind('input propertychange', function() {
    if (this.value == ""){
      $input.trigger("cleared");
    }
});
Ertug
  • 113
  • 1
  • 5
  • 1
    What if the user simply removed some text with his keyboard? – ghusse Jan 30 '15 at 09:06
  • @ghusse You can also clear a text field with a click and drag, without using that button. So accepted anwer will also fail. I guess OP asked the question because when a user presses clear field button, he wants to be notified with an event. – Ertug Jan 30 '15 at 13:01
  • With a click and a drag, I cannot see how to clear a text field except by pushing the del button of your keyboard. – ghusse Jan 30 '15 at 13:10
  • @ghusse Please select all of the text and try to drag to other text box. If that is not working for you try with Alt key pressed. – Ertug Jan 30 '15 at 18:24
  • seems that this is the better answer at the end of the day, though maybe not 100% specific to the question. Seems to cover all kinds of cases, even auto completes from chrome. – A.R. May 26 '15 at 19:59
8

I realize this question has been answered, but the accepted answer did not work in our situation. IE10 did not recognize/fire the $input.trigger("cleared"); statement.

Our final solution replaced that statement with a keydown event on the ENTER key (code 13). For posterity, this is what worked in our case:

$('input[type="text"]').bind("mouseup", function(event) {
    var $input = $(this);
    var oldValue = $input.val();
    if (oldValue == "") {
        return;
    }
    setTimeout(function() {
        var newValue = $input.val();
        if (newValue == "") {
            var enterEvent = $.Event("keydown");
            enterEvent.which = 13;
            $input.trigger(enterEvent);
        }
    }, 1);
});

In addition, we wanted to apply this binding only to the "search" inputs, not every input on the page. Naturally, IE made this difficult as well... although we had coded <input type="search"...>, IE rendered them as type="text". That's why the jQuery selector references the type="text".

Cheers!

OrangeWombat
  • 305
  • 3
  • 9
  • brilliant, worked for me, although i triggered keyup instead (my code looks for 2 chars and automatically searches on keyup) – punkologist Apr 10 '15 at 00:25
6

We can just listen to the input event. Please see the reference for details. This is how I fixed an issue with clear button in Sencha ExtJS on IE:

Ext.define('Override.Ext.form.field.ComboBox', {
    override: 'Ext.form.field.ComboBox',

    onRender: function () {
        this.callParent();

        var me = this;
        this.inputEl.dom.addEventListener('input', function () {
            // do things here
        });
    }
});
jaselg
  • 367
  • 3
  • 10
  • 1
    This answer works for MS Edge, the "input" event is fired when clicking the "x" to clear the search field. – Mottie Jan 15 '16 at 03:54
  • I'm using vanilla js and MS Edge doesn't fire the input event when clicking the X. I guess it was a bad idea anyway to try to watch such event. Better use a button like normal forms. – Rivenfall Oct 15 '18 at 16:48
3

An out of the box solution is to just get rid of the X entirely with CSS:

::-ms-clear { display: none; } /* see https://stackoverflow.com/questions/14007655 */

This has the following benefits:

  1. Much simpler solution - fits on one line
  2. Applies to all inputs so you don't have to have a handler for each input
  3. No risk of breaking javascript with bug in logic (less QA necessary)
  4. Standardizes behavior across browsers - makes IE behave same as chrome in that chrome does not have the X
Almenon
  • 813
  • 9
  • 16
1

for my asp.net server control

<asp:TextBox ID="tbSearchName" runat="server" oninput="jsfun_tbSearchName_onchange();"></asp:TextBox>

js

function jsfun_tbSearchName_onchange() {
    if (objTbNameSearch.value.trim() == '')
            objBTSubmitSearch.setAttribute('disabled', true);
        else
            objBTSubmitSearch.removeAttribute('disabled');
        return false;
}

ref

MSDN onchange event - tested in IE10.

... or to hide with CSS :

input[type=text]::-ms-clear { display: none; }
ablaze
  • 664
  • 7
  • 22
0

The above code was not working in my case and I have changed one line and introduced $input.typeahead('val', ''); which works in my case..

// There are 2 events fired on input element when clicking on the clear button:// mousedown and mouseup.
$("input").on('mouseup', function(e){
    var $input = $(this),
    oldValue = $input.val();
    if (oldValue === ''){
        return;
    }
    // When this event is fired after clicking on the clear button // the value is not cleared yet. We have to wait for it.
    setTimeout(function(){
        var newValue = $input.val();
        if (newValue === ''){
            $input.typeahead('val', '');
            e.preventDefault();
        }
    }, 1);
});
GitaarLAB
  • 13,494
  • 9
  • 51
  • 74
Maneesh Thareja
  • 51
  • 1
  • 1
  • 4