26

In HTML5 there is a new input type, 'search'. On most browser it's just remain to a simple 'text' input, but for webkit based browsers, it adds a little cross to reset the input.

I'd like to be able to handle this, is there an event for that?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Boris Guéry
  • 47,316
  • 8
  • 52
  • 87
  • More answers here http://stackoverflow.com/questions/2977023/how-do-you-detect-the-clearing-of-a-search-html5-input/35047611#35047611 – dlsso Jan 27 '16 at 20:56

10 Answers10

19

This mentions a comment from Ajaxian (WebKit HTML5 Search Inputs):

There are also some custom events too – such as ’search’ which fires when the user pauses typing (set ‘incremental’ to true).

When testing, I found that this 'search' event also gets called when the input is cleared (at least in the latest version of Safari).

Matthew Hegarty
  • 3,791
  • 2
  • 27
  • 42
Nick Wientge
  • 431
  • 3
  • 6
5

It seems miketaylr is correct, it isn't possible to catch this event. See the answer for this question: How do you detect the clearing of a "search" HTML5 input?

For the sake of being standards-compliant and not relying on one feature of one layout engine, I suggest you run your code on an onChange event, if the new text value is empty.

Community
  • 1
  • 1
n0nick
  • 1,067
  • 10
  • 21
  • The change event only fires after the text input has lost focus: if you are updating anything incrementally the user will expect it to update immediately after clicking the X button, rather than waiting until focus is lost. – Jonathan Morgan Sep 06 '13 at 01:18
3

You can listen for the "input" event too, this is more generic than "search" event but still catches the reset option.

case nelson
  • 3,537
  • 3
  • 30
  • 37
3

No, it's not possible. This UI interaction is some goodness that webkit implements, but is not actually specced. See here. So even if it were possible--you can't expect this UI to be implemented in Gecko, for example, if and when they ever add type=search.

miketaylr
  • 1,936
  • 3
  • 15
  • 14
  • This wouldn't be a problem, I mean with feature dectection, it'll just add nice a way to reset an input and dispatch some things on event. – Boris Guéry Mar 22 '10 at 15:18
  • Sure. Except there's no feature to detect--unless you're doing some browser sniffing for Webkit on top of feature detecting type=search. However, remember that there is no event for the feature you're describing so its a moot point. – miketaylr Mar 22 '10 at 15:55
2

I don't know that this is the correct answer, but the click event handler is called when the user clears the input using the X button.

$('input[type="search"]').click(function(event) {
    // This code runs anytime a user clicks on the input,
    // including when they clear it using the X button.
})
jpwatts
  • 8,297
  • 1
  • 22
  • 8
  • 7
    The problem is we have no way to know if the user is actually clicking on the cross to clean his input or to edit it. – Boris Guéry Aug 01 '10 at 20:07
  • 1
    I agree with Boris. Also, beware: IE 10 doesn't seem to fire a "click" event when its new X button is clicked - just mousedown and mouseup. Even after mouseup has been fired, the value still hasn't been changed to empty. – Jonathan Morgan Sep 06 '13 at 01:14
2

Am not sure, if still someone is looking for a solution. However below code snippet/tricks worked for me. CSS to complete hide the clear(X) icon and if you don't want to hide the clear(X) icon but need to handle then JS code snippet would help.

CSS trick:

#textFieldId::-ms-clear {display: none;} -- For a particular text field

or

input[type=text]::-ms-clear { display: none; } -- for all text fields in scope

OR

JavaScript trick (to reset the text field value to empty/blank and fire the html event KeyUp. Accordingly you can change per your need)

$('#textFieldId').bind("mouseup", function() {
        var $input = $(this);
        var oldValue = $input.val();
        if (oldValue == "") {
            return;
        }
        setTimeout(function() {
            var newValue = $input.val();
            if (newValue == "") {
                $input.trigger("keyup");
            }
        }, 1);
    });
Community
  • 1
  • 1
Sunil Kumar Das
  • 109
  • 1
  • 2
0

You could add a condition to check if the contents of the search field are empty but this is all no good even with a timeout because it runs the code first then clears the box. The other problem is that the code would not react to the close button when you are editing the field

0

I suppose u want to capture event when cross button is clicked Here is one solution I found I seems to be working fine(Without setTimeout())

$('id').bind('input propertychange', function() {
if (this.value == ""){
    doStuff()
    $input.trigger("cleared");
}});

Hope this helps!

Shubham Khare
  • 83
  • 1
  • 11
0

In my testing with MS Edge, it didn't fire any of these events. I tried input, change, click, and search and they weren't triggered when you click the X. I even tried directly setting input.onchange, input.onclick, etc - still nothing.

The only events that seemed to fire were mousedown and mouseup, but the input's value hadn't been cleared yet so I had to use a setTimeout after detecting the event.

GeekSharp
  • 93
  • 2
  • 10
-1

try this hope help you

$("input[name=search-mini]").on("search", function() {
 //do something
});