Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"input type=date" placeholder on iOS?

I'm currently developing a website aimed at iPhones/ iPads.

Setting the input type to date changes the field appearance to a drop down menu, and the placeholder attribute that works with text fields only shows on the desktop version, not on the iphone. Is there a way around this?

This is how it currently looks; http://i44.tinypic.com/2u91xsz.png

I want to get rid of that closing date text and have something similar to the dropdown menu above it.

Its basically just so that I can let the user know what they are entering without the need to use any extra space on the screen, so its not the biggest issue if there is no solution.

like image 420
David Avatar asked Mar 08 '12 17:03

David


People also ask

How do you set place holder in input type date?

The placeholder attribute does not work with the input type Date, so place any value as a placeholder in the input type Date. You can use the onfocus=”(this. type='date') inside the input filed.

How can I get current date in placeholder?

Try something like this: $('#fieldID'). attr('placeholder', new Date()); You will get the date and you can format it as required.

Does Safari support input type date?

Note: Date and time input types is Partially Supported on Safari 15, which means that any user who'd be accessing your page through Safari 15 can see it perfectly. Browser incompatibility may be due to any other web technology apart from Date and time input types.

How do you change the input type date in dd mm yyyy format?

To set and get the input type date in dd-mm-yyyy format we will use <input> type attribute. The <input> type attribute is used to define a date picker or control field. In this attribute, you can set the range from which day-month-year to which day-month-year date can be selected from.


3 Answers

Took me a while figuring this one out, leave it as type="text", and add onfocus="(this.type='date')"

i even like the onblur i've seen mentioned

<input placeholder="Date" class="textbox-n" type="text" onfocus="(this.type='date')" onblur="(this.type='text')" id="date">
like image 108
Dally S Avatar answered Sep 29 '22 23:09

Dally S


the placeholder attribute that works with text fields only shows on the desktop version, not on the iphone. Is there a way around this?

Try using javascript and css.

<div id="closing_date">
<label for="closing_date_field" class="overlabel">Closing Date</label>
<input id="closing_date_field" type="date" name="closing_date">
</div>

see the code here

like image 21
Jasonw Avatar answered Sep 29 '22 22:09

Jasonw


I've made it work with some ":before" pseudo-class and a small bit of javascript. You will have to have a class or id on your input. Here is a sample with an id of "myInput"

 #myInput:before{ content:"Date of birth"; width:100%; color:#AAA; } 
 #myInput:focus:before,
 #myInput.not_empty:before{ content:none }

Then in javascript, add the "not_empty" class depending on the value in the onChange and onKeyUp events.

You can even add all of this dynamically on every date fields.

Here is a rough code to do this :

$('input[type=date]').each(function(){
    var input=$(this);
    var id=input.attr('id');
    if (!id){
        id='RandomGeneratedId_'+Math.random().toString(36).substring(7);
        input.attr('id',id);
    }
    var placeholder=input.attr('placeholder');
    if (placeholder){
        $('head').append('<style> #'+id+':before{ content:"'+placeholder+'"; width:100%; color:#AAA; } #'+id+':focus:before,#'+id+'.not_empty:before{ content:none }</style>');
    }
    input.on('change keyup',function(){
        if ($(this).val()){
            $(this).addClass('not_empty')
        }else{
            $(this).removeClass('not_empty')
        }
        return false;
    });
});

It's not perfect but it's working well in Chrome and iOS7 webviews

like image 37
Guillaume Gendre Avatar answered Sep 29 '22 22:09

Guillaume Gendre