Solved

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

Posted on 2010-11-27
5
1,121 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…
Suggested Courses

626 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