Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable copy/paste on html5 date field

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.

like image 374
Karthikeyan Avatar asked Apr 23 '18 12:04

Karthikeyan


People also ask

How can change date format in dd mm yyyy in HTML?

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.

Are there any style options for the html5 date picker?

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.

How do I get the input type for a date?

<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.

What is date picker in HTML?

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!


2 Answers

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" />
like image 102
Red Avatar answered Oct 18 '22 13:10

Red


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.

Update

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">
like image 2
Lewis Avatar answered Oct 18 '22 12:10

Lewis