views:

167

answers:

4

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 ?

A: 

At first glance, I would say no there is not a way to capture it.

http://www.w3schools.com/html5/tag_input.asp

There appears to be no listed event for it since it shares events with all of other input types.

That being said, you could always just step through it in a browser and inspect the event object for some kind of identifier. Different browsers might handle it differently however.

NickLarsen
w3schools is the worse reference page for anything. From their footer, "We do not warrant the correctness of its content. The risk from using it lies entirely with the user." Best to use the spec instead.
miketaylr
I also read through the spec as well before posting this. I ended up listing that as the reference because the spec is not an easy beast to navigate for the lay passerby. In this case the information seems to be the same regarding the events available for input elements in HTML5.
NickLarsen
A: 

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
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
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
A: 

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
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
+1  A: 

It seems miketaylr is correct, it isn't possible to catch this event. See the answer for this question: http://stackoverflow.com/questions/2977023/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.

n0nick