2

I would like to trigger the display of a date input's date-picker from an external button.

<input id="date" type="date" />
<button>display date</button>

For instance if I had the above code, how would I show the date-picker (the box which appears and allows you to pick a date) by clicking the button? I do not want to use jQuery or other libraries. Is there a way to show the native date-picker from an external trigger with vanilla JavaScript?

I'm looking for something like this:

var button = document.querySelector("button");
button.onclick = () => {
    var input = document.querySelector("#date");
    input.showDatePicker();
}
1

2 Answers 2

1

This works well on firefox and edge :

<input id="date" type="date" />
<button>display date</button>
<script type="text/javascript">
	var button = document.querySelector("button");
button.onclick = () => {
    var input = document.querySelector("#date");
    input.focus()
    input.click()
}
</script>

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

1 Comment

Works on Chrome/Android and Firefox/Desktop but not on Chrome/Desktop nor Firefox/Android...
1

You can with

let x = document.getElementById('myDate'),
  d = new Date(),
  m = d.getMonth() < 10 ? `0${d.getMonth()}` : `${d.getMonth()}`,
  day = d.getDay() < 10 ? `0${d.getDay()}` : `${d.getDay()}`;

x.value = d.getFullYear() + "-" + m + "-" + day;

Demo

1 Comment

Hi so I didn't want a way to get the date but a way to make the date-picker show. The thing that appears when you click the arrow on pc, or on mobile just touch the input. I want to make that appear from a separate button

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.