6

I have a HTML5 design where I want to be able to use a button to display a date, but I want to use the HTML5 input type="date" to select the date.

Here is my code:

<button type="button" id="btnDate" class="btn btn-primary">
    <span class="glyphicon glyphicon-calendar"></span> <span class="date">27 Jul 2015</span>
</button>
<input type="date" class="hidden" id="tbDate" />

<script type="text/javascript">
    $(document).ready(function() {
        $("#btnDate").click(function () {
            $("#tbDate").focus();
        });
    });             
</script>

I've tried focus() and click() - neither work...any ideas?

6
  • Why the input is hidden ? Do you want to click on button and then show in input the button date ? Commented Sep 7, 2015 at 9:32
  • 1
    @Zl3n I don't want the input displayed at all - only the button, but I want the button to trigger the mobile's native date picker. Commented Sep 7, 2015 at 9:33
  • @series0ne Which mobile OS/browser are you using? Commented Sep 7, 2015 at 9:40
  • @Vucko - iOS, but it needs to work on Android and Windows Mobile too ideally Commented Sep 7, 2015 at 9:51
  • @series0ne well, type=date has a bed support (caniuse), especially for mobile browsers. I'd recommend that you try with this answer. Commented Sep 7, 2015 at 9:56

3 Answers 3

3

Please browse with your phone.

<label for="dateInput">click button</label>
<input type="date" id="dateInput" />

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

3 Comments

Err, there is no button in your example. There's only a label and a text box. Perhaps you missed a step somewhere?
Label tag instead of button.(From google translation)
There's no option to run code snippet when I opened it from mobile.
1

Try using showPicker() api:

const btn = document.getElementById("btn");
const dateComponent = document.getElementById("date");

btn.addEventListner("click", () => {
    dateComponent.showPicker();
})

Comments

0

You need set val() for input. Hope this help

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<button type="button" id="btnDate" class="btn btn-primary">
    <span class="glyphicon glyphicon-calendar"></span> <span class="date">27 Jul 2015</span>
</button>
<input type="date" class="hidden" id="tbDate" />

<script type="text/javascript">
    $(document).ready(function() {
        $("#btnDate").click(function () {
          var date = new Date("2015, 07, 27");
          var setDate = date.getFullYear() + "-" + ("0" + (date.getMonth() + 1)).slice(-2) + "-"+("0" + date.getDate()).slice(-2);
            $("#tbDate").val(setDate);
        });
    });             
</script>

3 Comments

Why is this the answer? This does not trigger the <input='date' /> in order to select a date as asked in the question.
he want a button to input date. I understand like that.
He wants a button to display the date but he said "I want to use the HTML5 input type="date" to select the date."

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.