Is there a way to convert a text type input field to a date type one with jQuery, so it will display to the user a date picker? I can add custom fields to a form created by a Wordpress plugin, but it doesn't support date nor datetime-local field types, and I need a such one.
<input name="adverts_eventDate" id="adverts_eventDate" type="text">
You can easily change the type in jQuery:
$("#adverts_eventDate").attr("type","date");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="adverts_eventDate" id="adverts_eventDate" type="text">
You can't change date format in HTML5 date:
Is there any way to change input type="date" format?
Since you can't change your date format in HTML5, I would recommend jQuery-UI.
Here's an example:
$( function() {
$( "#adverts_eventDate" ).datepicker();
} );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="http://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet"/>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<p>Date: <input name="adverts_eventDate" id="adverts_eventDate" type="text"></p>
convert a text type input field in a date type
You can use jquery's attr to change the type attribute from text to date.
$("#convert").click(function() {
$("#adverts_eventDate").attr("type", "date")
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="adverts_eventDate" id="adverts_eventDate" type="text">
<button id='convert'>to date</button>
You can use jquery-ui as follows
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"> </script>
<input name="adverts_eventDate" id="adverts_eventDate" type="text">
and then
<script>
$( function() {
$( "#adverts_eventDate" ).datepicker();
} );
</script>