noob'88
noob'88

Reputation: 117

datepicker date format...I've read the other posts

I am trying to re-format the date input that I am getting from my date picker.....

Here is the code that I have within a section of the body

<div class="simpleTabsContent">PLACEHOLDER: Linux Update
           Date: <input type="text" name="datepicker" id="datepicker">
           <br>
           <button type="button" onclick="myFunc2()">Button Text...click me </button>
           <p id="test"></p>
           <script>
              function myFunc2(){
                var x;
                x = $(".selector").datepicker({dateFormat: 'yy-mm-dd'}).val();
                //x = document.getElementById("datepicker").value;
                document.getElementById("test").innerHTML = x;
              }
           </script>
     </div>

This is the code I have within my head...

 <head>
  <meta charset="utf-8">
  <title>SPUI Development</title>
  <style type="text/css" media="screen">
     @import "css/style.css";
  </style>
  <!-- SimpleTabs -->
  <script type="text/javascript" src="js/simpletabs_1.3.js"></script>
  <style type="text/css" media="screen">
     @import "css/simpletabs.css";
  </style>
  <!-- Code for calendar -->
  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script>
  $(function() {
     $.datepicker.setDefaults(
        $.extend ($.datepicker.regional[''])
     );
     $('#datepicker').datepicker();
   });
  </script>

I can't tell what I am doing wrong. Someone please help? I also did read the api and several stack overflow examples along with online examples....I am lost.

Upvotes: 0

Views: 253

Answers (3)

Barmar
Barmar

Reputation: 781058

You can only provide options like dateFormat: when you first initialize the datepicker, which is in the $(function() {...}). It should be:

$(function() {
    $.datepicker.setDefaults(
        $.extend ($.datepicker.regional[''])
    );
    $('#datepicker').datepicker({
        dateFormat: 'yy-mm-dd'
    });
});

Then your other function can be:

function myFunc2(){
    var x;
    x = $("#datepicker").val();
    $("#test").text(x);
}

You don't need to call .datepicker() here. When the user selects something from the datepicker, it's put into the value of the input element.

If you want to change the options of a jQuery UI widget on the fly after initialization, you do it using the option or options methods:

$("#datepicker").option("dateFormat", "yy-mm-dd");

But this doesn't seem necessary for what you're doing.

Upvotes: 0

Rodney G
Rodney G

Reputation: 4826

You CAN alter the format after your datePicker is instantiated. You just forgot the keyword option:

x = $("#datepicker").datepicker('option', 'dateFormat', 'yy-mm-dd').val();

Upvotes: 1

Tomanow
Tomanow

Reputation: 7367

I see right away that you don't have an element with class selector

x = $(".selector").datepicker({dateFormat: 'yy-mm-dd'}).val();

Did you mean

x = $("#datepicker").datepicker({dateFormat: 'yy-mm-dd'}).val();

Upvotes: 0

Related Questions