2

I want to get the value of a date input field and alert it,but I always get an empty string or undefined. Any help on how to get the value of this field will be appreciated.

code -

var end = $('#datetimepicker2').val();

$('#sub').on('click', function () {
    alert(end);
});

HTML CODE

<input type="text" class="form-control input-group-sm" placeholder="Enter Date" id="datetimepicker" aria-label="...">

I am using bootstrap-datepicker plugin to get the date. EDIT: I want to get the date in yyyy/mm/dd format and store it in a variable

8
  • 1
    post the html code for the datepicker Commented Oct 25, 2017 at 15:38
  • when var end line get executed? Commented Oct 25, 2017 at 15:38
  • @GabrielMesquita I just made the edit now Commented Oct 25, 2017 at 15:45
  • your id is datetimepicker and in jquery you have datetimepicker2, is that it? Commented Oct 25, 2017 at 15:47
  • @GabrielMesquita I have two input field for date. I copied the wrong one . But the code is correct. Commented Oct 25, 2017 at 15:49

3 Answers 3

3

When var end = $('#datetimepicker2').val(); gets executed, the <input id="datepicker2"> element has no value.

It remains empty because you have not defined an event handler to update it when the value has changed. The following should be a working example of what you are trying accomplish.

<table>
    <tr>
        <td><strong>Date Time Picker:</strong></td>
        <td>
            <input id="datetimepicker2" class="datetimepicker" value=""/>
        <td>
        <td><button id="sub">Submit</button></td>
    </tr>
</table>

<script>
    // If the datepicker2 input is empty, this will be empty, even after you change it.
    var end = $('#datetimepicker2').val();
    $('#datetimepicker2').datetimepicker();

    // This will update the "end" variable as it changes.
    $(document).on('change', '#datetimepicker2', function() {
        end = $(this).val();
    });

    $(document).on('click', '#sub', function () {
        alert(end);
    });
</script>

ps: I changed the $(x).on(event,function) to $(document).on(event,element,function) because I've had better results with this as the DOM is manipulated over time.

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

2 Comments

you saved my day
I am glad that I could help. :)
0
 $('#datePicker').datepicker()
        .on("input change", function (e) {
            //$('.loadingBackground').show();

            var date = $(this).datepicker('getDate');
alert(date);

});

This will alert the date every time the date is changed.

Or on the click event:

var date = $('#datePicker').datepicker('getDate');
$('#sub').on('click', function () {
    alert(date);
});

Comments

0

You must use the same id in your jquery like this:

$('document').ready(function(){
    $('#sub').on('click', function () {
        var end = $('#datetimepicker').val()
        alert(end);
    });
)}

And you probably want to get the value after you select the date and click the #sub button.

1 Comment

You want to get the value after you click the #sub button? if it is put the var end declaration inside the click callback.

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.