Nur Wafiqa
Nur Wafiqa

Reputation: 93

Retrieving Value from Datepicker

I encounter this problem. What I want to do is, I want to retrieve the value of the datepicker and store it in the variable, then I wanted the value to popup. However, when I click Go button , I got the undefined as the value.Here's my code :

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery UI Datepicker - Default functionality</title>
   <link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
  <script src="js/jquery-1.7.1.min.js"></script>
  <script src="js/jquery-ui.js"></script>
  <script src="js/jquery-ui.min.js"></script>

  <script>
  $(function() {
    $( "#datepicker" ).datepicker({
        changeMonth: true,
        changeYear: true});
  });
  </script>
  <style>
    div.ui-datepicker{font-size:10px;}
  </style>
</head>
<body>

<p>Date: <input type="text" id="datepicker"></p>
<input value="  Go  " onClick="Go_OnClick()" type="button">

<script>
    function Go_OnClick(){
        dateVariable = $("datepicker").val();
        alert(dateVariable);
    }
</script>


</body>
</html>

Upvotes: 0

Views: 84

Answers (3)

Sandeep
Sandeep

Reputation: 605

you are missing # when we are trying to get the val of datepicker.

So use $("#datepicker").val(); in Go_OnClick() methos and it will work

Upvotes: 1

Stanimir Dimitrov
Stanimir Dimitrov

Reputation: 1890

You have a typo.

Change dateVariable = $("datepicker").val(); to dateVariable = $("#datepicker").val();

You were missing one # before datepicker

Upvotes: 1

Dhaval Marthak
Dhaval Marthak

Reputation: 17366

You missed # for IDselector, and that might not be good practice to retrieve date with using .val() you'll get string value, to retrieve date:

You need to use getDate()

function Go_OnClick(){
    dateVariable = $("#datepicker").datepicker('getDate'); //getDate and # for ID selectror
    alert(dateVariable);
 }

Upvotes: 2

Related Questions