Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Placeholder text for an input type="number" does not show in webkit ICS

Tags:

html

android

I have the following input field

<input type="number" id="zip-code" placeholder="Zip code" />

Placeholder is shown in normal browsers except android 4.0 and above and the width also get reduced compared to other fields.
What could be the error?

like image 421
Shinoj Avatar asked Jul 20 '12 09:07

Shinoj


People also ask

How do you use placeholder input?

The placeholder attribute specifies a short hint that describes the expected value of an input field (e.g. a sample value or a short description of the expected format). The short hint is displayed in the input field before the user enters a value.

How do you add a placeholder to a text box?

If you want to set a hint for text area or input field, then use the HTML placeholder attribute. The hint is the expected value, which gets displayed before the user enters a value, for example, name, details, etc.

What is a placeholder element?

Definition and Usage The placeholder attribute specifies a short hint that describes the expected value of a input field / textarea. The short hint is displayed in the field before the user enters a value.

What is a placeholder code?

A placeholder in programming code may also be used to indicate where specific code needs to be added, but the programmer has not yet written the code. The placeholder reminds the programmer where to add code, or can let other programmers know that additional code still needs to be added in general.


2 Answers

It's a known bug in the default Android Webkit browser. As per this QuirksMode page, you can see that many versions of Android suffer from this. Chrome for Android on the other hand, doesn't.

I've created a simple JavaScript shim (fix) for this. First, read this question How to display placeholder's text in HTML5's number-typed input, then if you still want to use a placeholder like that, checkout my solution gist.

TL;DR;

Leave your markup as you would expect;

<input type="number" placeholder="Enter some numbers" />

Then run either of these scripts after the page has loaded;

// jQuery version
$("input[type='number']").each(function(i, el) {
    el.type = "text";
    el.onfocus = function(){this.type="number";};
    el.onblur = function(){this.type="text";};
});

// Stand-alone version
(function(){ var elms = document.querySelectorAll("input"), i=elms.length;
    while(i--) {
        var el=elms[i]; if(el.type=="number"])
            el.type="text",
            el.onfocus = function(){this.type="number";},
            el.onblur = function(){this.type="text";};
    }
})();
like image 154
aaronsnoswell Avatar answered Nov 15 '22 15:11

aaronsnoswell


By using type = "tel' instead of type = "number" the placeholder is displayed and the numeric keyboard is opened on focus.

like image 20
David Sanchez Avatar answered Nov 15 '22 16:11

David Sanchez