Solved

jQuery UI DatePicker: How to add multiple alternate fields & ordinal suffix to Date

Posted on 2010-11-27
5
1,076 Views
Last Modified: 2012-05-10
11/28/10

Greetings Everyone;

I'm a newbie at this, returning for more punishement, so please bear with me...  :)

I have a working jQuery UI DatePicker script that only
displays WED & SAT with a Button TRIGGER SCRIPT (THANKS roynaufal! : see MAINSCRIPT BELOW).

I'm trying to add multiple (2) alternate input fields and an ordinal suffix (th, rd, nd, st) behind the
date value in the mainscript below.

The alternate code works when one alternate input field is added; but when 2 (or more)
are added it only populates the 1st input and last alternate input field.

When the suffix javascript calculates, it only returns the word "suffix" behind the date value
(not the var suffix value).

What am I missing?

Thank you in advance for your time and consideration.
´*•.¸(`*•.¸?¸.•*´)¸.•*´
?*°Doris 42DoubleDDs*°•´»?
.¸.•*(¸.•*´?`*•.¸) *•.¸


<Code>

<MAIN SCRIPT>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Disable Certains Days in a Week using jQuery UI DatePicker</title>
<link rel="stylesheet" href="css/Redmond/jquery-ui-1.8.6.custom.css" type="text/css" />
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.6.custom.min.js"></script>
<script type="text/javascript">
                  var d = new Date();
                  var current_date = d.getDate();
                  var current_month = d.getMonth();
                  var current_year = d.getFullYear();
                  var suffix = "";
                  if (current_date == 1 || current_date == 21 || current_date ==31)
                     {
                     suffix = "st";
                     }
                  else if (current_date == 2 || current_date == 22)
                     {
                     suffix = "nd";
                     }
                  else if (current_date == 3 || current_date == 23)
                     {
                     suffix = "rd";
                     }
                  else
                     {
                     suffix = "th";
                     }
      $(function() {
            $( "#datepicker" ).datepicker({
                  showOn: "button",
                  buttonImage: "css/Redmond/images/calendar.gif",
                  buttonImageOnly: true,
                  altField: "#alternate",
                  altFormat: "DD, MM d"+"suffix, "+" yy",
                  beforeShowDay: function(day) {
            var day = day.getDay();
            if (day == 0 || day == 1 || day == 2 || day == 4 || day == 5) {
                return [false, "somecssclass"]
            } else {
                return [true, "someothercssclass"]
            }
        }
            });
      });
</script>

</head>
<body>
<p>Date: <input type="text" id="datepicker">&nbsp;<input type="text" id="alternate" size="30"/>&nbsp;<input type="text" id="alternate2" size="30"/></p>
</body>
</html>

<MAIN SCRIPT>

</Code>
0
Comment
Question by:DMackallii
  • 3
  • 2
5 Comments
 
LVL 18

Expert Comment

by:Sudaraka Wijesinghe
ID: 34226425
You will need to change the altField and altFormat parameters like this;

altField: "#alternate,#alternate2", //Id of each input you need to fill with the selected date

altFormat: "DD, MM d"+suffix+", "+" yy",

Open in new window

0
 

Author Comment

by:DMackallii
ID: 34227947
Thank You for the prompt response Sudaraka.

Your script solution worked perfectly, but revealed a flaw in my script

My suffix script is pulling the d date value from the computers clock, and not f'rom the d date value selected on the jQuery UI Datepicker.

Any suggestions on how I can pull this  jQuery UI Datepicker d value to calculate the date suffix?

  altFormat: "DD, MM d"+suffix+", "+" yy",

Thank you in advance for your time and consideration.
´*•.¸(`*•.¸?¸.•*´)¸.•*´
?*°Doris 42DoubleDDs*°•´»?
.¸.•*(¸.•*´?`*•.¸) *•.¸
0
 
LVL 18

Accepted Solution

by:
Sudaraka Wijesinghe earned 500 total points
ID: 34230272
Check this out
<script type="text/javascript">

      $(function() {

            $( "#datepicker" ).datepicker({

                  showOn: "button",

                  buttonImage: "css/Redmond/images/calendar.gif",

                  buttonImageOnly: true,

                  altField: "#alternate,#alternate2",

                  onSelect: function(text, inst){

					  var d = new Date(text);

					  var current_date = d.getDate();

                  var suffix = "";

                  if (current_date == 1 || current_date == 21 || current_date ==31)

                     {

                     suffix = "st";

                     }

                  else if (current_date == 2 || current_date == 22)

                     {

                     suffix = "n'd'";

                     }

                  else if (current_date == 3 || current_date == 23)

                     {

                     suffix = "r'd'";

                     }

                  else

                     {

                     suffix = "th";

                     }

					  

					  $( "#datepicker" ).datepicker('option', 'altFormat', "DD, MM d"+suffix+", yy");

				  },

                  beforeShowDay: function(day) {

            var day = day.getDay();

            if (day == 0 || day == 1 || day == 2 || day == 4 || day == 5) {

                return [false, "somecssclass"]

            } else {

                return [true, "someothercssclass"]

            }

        }

            });

      });

</script>

Open in new window

0
 

Author Closing Comment

by:DMackallii
ID: 34236652
11/30/10

Thanks again for the prompt response Sudaraka.

That's exactly what I was looking for!

I never would have figured that one out on my own.

You make it look easy.

Thanks again for your time and consideration. Take care.
´*•.¸(`*•.¸?¸.•*´)¸.•*´
?*°Doris 42DoubleDDs*°•´»?
.¸.•*(¸.•*´?`*•.¸) *•.¸
0
 
LVL 18

Expert Comment

by:Sudaraka Wijesinghe
ID: 34236693
Glad to help. Thanks for the points.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Show confirmation alert in Jquery 14 46
jQuery Parallax 1 38
How can I   ajax html table  rows? 20 64
Elegant Way to Include Query String When it exists? 4 43
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now