Am able to copy values from textbox and paste into another textbox in my html5 form. Same way how can i copy value from the date field.
<input type="date" />
I want to copy value from one date field and paste it to another date field.
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.
Currently, there is no cross browser, script-free way of styling a native date picker. As for what's going on inside WHATWG/W3C... If this functionality does emerge, it will likely be under the CSS-UI standard or some Shadow DOM-related standard.
<input type="date"> <input> elements of type="date" create input fields that let the user enter a date, either with a textbox that validates the input or a special date picker interface. The resulting value includes the year, month, and day, but not the time.
The <input type="date"> defines a date picker. The resulting value includes the year, month, and day. Tip: Always add the <label> tag for best accessibility practices!
By native?
No, a date input
field behaves differently than a text input
field.
Workaround
I had the same problem once and created a workaround.
When you dlbclick
the input field, it temporarily changes itself to a text
input field and automatically select its value. So you can copy the date by using CTRL + C
It also works when you want to copy a date from and text field into the date input field.
Register a focusout
event to reset the input to its original state type="date"
.
// get all date input fields
let dateInputs = document.querySelectorAll('[type="date"]');
dateInputs.forEach(el => {
// register double click event to change date input to text input and select the value
el.addEventListener('dblclick', () => {
el.type = "text";
// After changing input type with JS .select() wont work as usual
// Needs timeout fn() to make it work
setTimeout(() => {
el.select();
})
});
// register the focusout event to reset the input back to a date input field
el.addEventListener('focusout', () => {
el.type = "date";
});
});
input {
display: block;
width: 150px;
}
<label>Double click me</label>
<input type="date" value="2011-09-29" />
<input type="date" placeholder="paste the date here" />
So, you can do this with jQuery using the copy
and paste
events to take the value from one and insert it in to another using a fake clipboard.
Note: I've just found a weird quirk with this. If you click on the first date box and type a date, you then need to click OFF the input before copying. The same goes for pasting in to the second box. I can't figure out why this is.
var dateClipboard;
$("input[type='date']").on("copy", function(){
dateClipboard = $(this).val();
alert("copied");
})
$("input[type='date']").on("paste", function(){
if(dateClipboard != ''){
$(this).val(dateClipboard);
alert("pasted");
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<input type="date" name="test" id="test">
<input type="date" name="test" id="test2">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With