Solved

Jquery DatePicker

Posted on 2015-02-06
7
142 Views
Last Modified: 2015-02-07
I'm trying to use Jquery datepicker in an html page.

I took the example off the Jquery API, adjusted it & ran in on my web server. My revised version is attached; It works perfectly.

Then I adapted my "real" page to use it; here is the Jquery part of that page:

<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>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="jquery/jquery.lightbox_me.js"></script>
      
<script>
$(function() {
$( "#adddate" ).datepicker();
});
$(function() {
$( "#datecompl" ).datepicker();
});
</script>

In the html:

<td width="12%"><input type="text" name="adddate" id="adddate" size="10"></td>

and

<td><input name="datecompl" id="datecompl" type="text" class="pt12" size="12" readonly="readonly"></td>

When you click the mouse on either input field, nothing happens.

In the FireFox Webconsole, it says:

TypeError: $(...).datepicker is not a function
referring to the line $( "#adddate" ).datepicker();

What's wrong?
jq-datepicker-test.htm
0
Comment
Question by:Richard Korts
7 Comments
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 40595116
There's no reason to have separate init functions for jquery so change your code to this:

$(function() {
    $( "#adddate" ).datepicker();
    $( "#datecompl" ).datepicker();
});
0
 
LVL 15

Expert Comment

by:eemit
ID: 40595161
What happens if you don't use Scheme-less URL?
It seams that this does not work if you open the html file locally!
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 40595165
Yes it won't work locally because of the scheme-less URL.  If you view it locally it will try to load "file://code.jquery....."
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 9

Expert Comment

by:Brian Tao
ID: 40595585
Remove the line:
<script src="//code.jquery.com/jquery-1.10.2.js"></script>

Open in new window

As you have another script tag that includes jquery-latest.min.js.

And add "http:" to the other 2 lines where you have "//code........." and make them "http://code......"

then it should work.
0
 

Author Comment

by:Richard Korts
ID: 40595842
To Brian Tao,

I believe I did what you said. Doesn't work.

So I applied the same changes to the test page (that previously worked), now it doesn't work.

The revised version of that is attached.

FireFox web console gives this:

ReferenceError: jQuery is not defined jquery-ui.js:14
ReferenceError: $ is not defined

If we can get the test version to work, maybe I can try the real page

Thanks
jq-datepicker-test.htm
0
 

Author Comment

by:Richard Korts
ID: 40595862
To all,

I got the test version to work by adding this back in:

<script src="http://code.jquery.com/jquery-1.10.2.js"></script>

I also added it to the "real" page; real page does not work.

Here are the applicable elements of the real page:

<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="jquery/jquery.lightbox_me.js"></script>
      
<script>
            
                  $(document).ready(function() {
                  
                        $('#button').click(function(e) {
                              $('#as_built').lightbox_me({
                                    closeClick:      true,      
                              });
                              e.preventDefault();
                        });                        

                  });
$(function() {
    $( "#adddate" ).datepicker();
    $( "#datecompl" ).datepicker();
});
                  

    </script>

HTML:

<tr>
            <td align="right">Date Completed:&nbsp;</td>
            <td><input name="datecompl" id="datecompl" type="text" class="pt12" size="12" readonly="readonly"></td>
      </tr>

FireFox Web Console says:

TypeError: $(...).datepicker is not a function (refers to the line  $( "#adddate" ).datepicker();)

Note the lightbox thing has nothing to do with this, I included it in case it was somehow connected to making it not work.

Thanks
0
 
LVL 42

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 40595943
You're including a second copy of jQuery AFTER the jQuery UI, so that will break the UI and it won't work. You need to make sure jQuery is included only once, and is included BEFORE the UI.

If this is to be run from a webserver, then it's best-practice to NOT include the protocol in the src.

You also don't need a separate $(function) block, as you've already got a document.ready - they both do the same thing. Here's a summary of what your page should look like (including the specific order of the JS includes). Tested and working fine.

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8">
		<title>Chris Stanyon</title>

		<script src="//code.jquery.com/jquery-latest.min.js"></script>
		<script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
		<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
		
		<script type="text/javascript">
		$(document).ready(function() {
			$("#datecompl").datepicker();			
		});
		</script>	
	</head>
	<body>
		<input name="datecompl" id="datecompl" type="text" class="pt12" size="12" readonly="readonly">
	</body>
</html>

Open in new window

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

by Julian Matz As of jQuery (http://jquery.com/) 1.4 the .live() method (http://api.jquery.com/live/) supports custom events as well as some standard JavaScript events that it previously didn't support. Among these is the submit event. Unlike …
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn how to dynamically set the form action using jQuery.
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…

746 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

11 Experts available now in Live!

Get 1:1 Help Now