12

I am trying to change type of input, specifically after click into input I need to change type="text" to type="date".

<input type="text" placeholder="DOB" id="dob" name="dob" />
<script>
    $("#dob").click(function () {
        $(this).attr('type', 'date');

    });
</script>

I want to accomplish this for a iphone App. This is not working in iphone 4. How can do this?

5
  • 2
    Can I ask you reason for wanting to do this? I am pretty sure there must be a better way. Commented Aug 8, 2012 at 6:21
  • In iphone 4 the placeholder doesnot show for input type dat. So I want to change the type after clicking the field Commented Aug 8, 2012 at 6:23
  • Better have two input fields and hide the text field and show the date field on click event. Commented Aug 8, 2012 at 6:25
  • You don't need a placeholder on a date input - they come with widgets to do the formatting for you. The problem is that you appear to be using the placeholder as a replacement for a <label> which the specification is very explicit that you should not do. Commented Aug 8, 2012 at 6:34
  • The placeholder attribute should not be used as an alternative to a label. and The placeholder attribute represents a short hint (a word or short phrase) intended to aid the user with data entry. A hint could be a sample value or a brief description of the expected format. Commented Aug 8, 2012 at 6:34

6 Answers 6

33

Use this code :

<input type="text" placeholder="DOB" id="dob" name="dob"  />
<script>
   $("#dob").click(function(){
        $(this).prop('type', 'date');

  });
</script>

Here is the fiddle : http://jsfiddle.net/HNKkW/

Sign up to request clarification or add additional context in comments.

Comments

7

It's a property, so prop() will do it :

$("#dob").on('click', function(){
     $(this).prop('type', 'date');
});​

And remember to wrap that in a document.ready function !

FIDDLE

Comments

0

If you use Earlier version of Jquery

$('body').delegate('#dob','click',function(){
            $(this).attr('type', 'date');

      });

Or if you use latest see @adeno 's answer

Comments

0

See this working jsfiddle.

You can't change the type of an input due to restrictions in the browser using .attr. Note this warning you receive from the console:

Uncaught Error: type property can't be changed 

You can change the type by using .prop()

$("#dob").on('click', function(){
    $(this).prop('type', 'date');
});​

I think there may be a bit of an underlying problem here. Would the user not be confused by the type of text-box changing?

If at all possible you should try and change the type of the text-box before showing it to the user.

Comments

0

I think the correct solution for your problem is using the .focus and the .prop properties.

<script>
    $("#dob").focus(function () {
        $(this).prop('type', 'date');

    });
</script>

Comments

0

for me using prop directly on the element was not working, this however worked for me after a lot of fiddling, so I wanted to share and maybe save someone the headache.

$(document).on('change', '#showPass', function () {
    if ($(this).is(':checked')) {
        $(this).parents('form').find('#passField').prop("type", "text");
    } else {
        $(this).parents('form').find('#passField').prop("type", "password");
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.