Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change type of an Input element using jquery

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?

like image 863
Sreehari Avatar asked Aug 08 '12 06:08

Sreehari


2 Answers

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/

like image 174
Nishu Tayal Avatar answered Oct 17 '22 23:10

Nishu Tayal


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

like image 44
adeneo Avatar answered Oct 17 '22 23:10

adeneo