How to get Date and Time Picker values from textbox?

manigandand picture manigandand · Jan 15, 2015 · Viewed 7.5k times · Source

I am using date and time picker that not used for getting current date and time instead that is to for user want to select some date and time so for...

This is my code for date and time picker.

<form class="form-horizontal" id="myForm">
                            <label for="from" class="col-sm-1 col-md-1 col-xs-3 control-label">From:</label>
                            <div class="form-group ">
                                <div class="input-group">
                                    <span class="input-group-addon"> <i
                                        class="fa fa-time"></i>
                                    </span> <input type="text" name="from_time" id="from_time"
                                        class="form-control" placeholder="" readonly="" />
                                </div>
                            </div>
<label for="to" class="col-sm-1 col-md-1 col-xs-3 control-label">To:</label>
                    <div class="form-group ">
                        <div class="input-group">
                            <span class="input-group-addon"><i
                                class="fa fa-time"></i> </span> <input type="text"
                                name="to_time" id="to_time" class="form-control" placeholder=""
                                readonly="">
                        </div>
                    </div>
<label for="date" class="col-sm-1 col-md-1 col-xs-3 control-label">Date:</label>
                    <div class="form-group ">
                        <div class="input-group">
                            <span class="input-group-addon"><i
                                class="fa fa-cal"></i> </span> <input type="text"
                                name="date" id="date" class="form-control" placeholder="" />
                        </div>
                    </div>
<input type="button" onclick="getresult()" value="Submit">
</form>
                    </div>
                </div>
            </div>

and my script code for date and time picker

<script type="text/javascript">
        $(document).ready(function(){
        $('#from_time').timepicker({showMeridian: false});
        $('#to_time').timepicker({showMeridian: false});
        });
</script>
    <script type="text/javascript">
        $(document).ready(function(){
        $('#date').datepicker({ dateFormat: 'mm-dd-yy' }).datepicker("setDate", new Date());

        });
    </script>

and here is how i try to get that result of date and time from text box id

function getresult(){
    var Date=$("date").val();
    var From_Time=$("from_time").val();
    var To_Time=$("to_time").val();
    alert(Date+" "+From_Time+" "+To_Time);
}

but this gives result like "undefined" "undefined"..in don't know what to do plzzz help me out from this

Answer

Sadikhasan picture Sadikhasan · Jan 15, 2015

You have to use ID selector # sign to get value.

function getresult(){
    var Date=$("#date").val();
    var From_Time=$("#from_time").val();
    var To_Time=$("#to_time").val();
    alert(Date+" "+From_Time+" "+To_Time);
}

Manual