The JavaScript date picker is malfunctioning in the HTML editor, but it functions properly in Fiddle

I have a working format available in a JS fiddle. Here is the code I have used on my demo site: I created a new folder named "js" and placed datepicker.js inside it, then linked it in my HTML like this:

<script type="text/javascript" src="js/datepicker.js"></script>

My datepicker.js code looks like this:

$(function () {
    $('#departure-date').datepicker({
        numberOfMonths: 2,
        showAnim: "fold",
        showButtonPanel: true,
        onSelect: (function (date) {
            setTimeout(function () {
                $('#return-date').datepicker('show');
            }, 300)
            $(this).val($(this).datepicker('getDate').toLocaleDateString());
        })
    });

    $('#return-date').datepicker({
        numberOfMonths: 2,
        showAnim: "fold",
        showButtonPanel: true,
        onSelect: (function (date) {
            $(this).val($(this).datepicker('getDate').toLocaleDateString());
        })
    });
});

Below is my HTML code:

<input id="departure-date" type="text" placeholder="Depart Date" >
<input type="text" id="return-date" placeholder="return Date">

However, when I press the above button, the .js file is not being called. Any assistance would be greatly appreciated.

Answer №1

To successfully use the datepicker functionality, you must ensure that jQuery is included in your project. You can download the latest version of jQuery and place it in your js folder, linking it as you did with datepicker.js. Alternatively, you can directly include it using the following code:

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>

If this approach does not work, you can try including these specific versions:

<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

If there are any issues with your current datepicker.js file, switching to the jquery-ui js may solve them. I have personally tested this method with the specified sources, and it worked correctly.

If directly linking to the jQuery site's js files still does not resolve the problem, there might be additional issues on your page that we are not able to see.

Answer №2

Everything is running smoothly on my computer with your code. I just utilized the CDN paths for jQuery and jQueryUI from the Google Developer Site.

Your provided code snippet:

<!DOCTYPE html>
<html>
<head>
    <title>JS Datepicker</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
    <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
</head>
<body>
<input  id="departure-date" type="text" placeholder="Depart Date" >
<input type="text" id="return-date" placeholder="return Date">
<script type="text/javascript">
    $(function () {
    $('#departure-date').datepicker({
        numberOfMonths: 2,
        showAnim: "fold",
        showButtonPanel: true,
        onSelect: (function (date) {
            setTimeout(function () {
                $('#return-date').datepicker('show');
            }, 300)
            $(this).val($(this).datepicker('getDate').toLocaleDateString());
        })
    });

    $('#return-date').datepicker({
        numberOfMonths: 2,
        showAnim: "fold",
        showButtonPanel: true,
        onSelect: (function (date) {
            $(this).val($(this).datepicker('getDate').toLocaleDateString());
        })
    });
});
</script>
</body>
</html>

Answer №3

Make sure you don't misplace the position of jQuery, or else it won't work as expected. Here is some code that may assist you:

$(function () {
    $('#departure-date').datepicker({
        numberOfMonths: 2,
        showAnim: "fold",
        showButtonPanel: true,
        onSelect: (function (date) {
            setTimeout(function () {
                $('#return-date').datepicker('show');
            }, 300)
            $(this).val($(this).datepicker('getDate').toLocaleDateString());
        })
    });

    $('#return-date').datepicker({
        numberOfMonths: 2,
        showAnim: "fold",
        showButtonPanel: true,
        onSelect: (function (date) {
            $(this).val($(this).datepicker('getDate').toLocaleDateString());
        })
    });
});
     <link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css">
<script type="text/javascript" src="//code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="//code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<link rel="stylesheet" type="text/css" href="//code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css">

<input  id="departure-date" type="text" placeholder="Depart Date" >
<input type="text" id="return-date" placeholder="return Date">

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Transferring application configurations across modules

Questioning my current approach, I am developing a model & collection package (which exposes mongodb results as a model) and aiming for a modular structure. However, within the models, there are hardcoded settings like host, port, password, etc., which ...

Implementing XOR operation in a jQuery script

In need of a small script modification where one value needs to be XORed. A previous suggestion no longer applies due to changes made in the script. <input type='button' value='Ein / Aus' class='taster' csv='Lampe&apo ...

Creating personalized fonts in SAPUI5 Theme Designer

Recently, I have been working on creating a personalized theme for the HANA cloud platform Portal. In order to give my site a unique touch, I decided to incorporate custom fonts. To achieve this, I uploaded the fonts as an HTML application in HCP. Now, my ...

Instructions on submitting a form containing multiple select lists using the enter key

After researching various threads, I have come across solutions using the JS onkeypress function to trigger actions with input fields. However, in my case, I need to implement this functionality with a form that consists of multiple select lists instead of ...

Get the application/pdf document that was just sent to you from the software backend

I am facing an issue with downloading a PDF file sent from the backend. Upon receiving a blob response, I notice that when I download and view the file, the sheets are empty, matching the expected number of sheets. Could this be a coding problem? Current ...

Creating a multi-level mobile navigation menu in WordPress can greatly enhance user experience and make

Hey there, I am currently in the process of developing a custom WordPress theme and working on creating a mobile navigation system. Although I have managed to come up with a solution that I am quite pleased with after multiple attempts, I have encountered ...

Steps for eliminating QRcode warning in npmjs package

Issue: Warning Message (node:24688) ExperimentalWarning: buffer.Blob is an experimental feature. This feature could change at any time (Use `node --trace-warnings ...` to show where the warning was created) Seeking Solution: How can I prevent this warning ...

Automatically adjusting jQuery script now functions on all hyperlinks for seamless resizing

I am attempting to create a rollover script that will resize my social network links when hovered over. The desired effect is for the 32px square icon to expand and display the trailing address. For example, when hovering over the Facebook link, it should ...

How to apply gradient colors to borders using CSS

Is it possible to create a simple border bottom color with a gradient effect? div{ border-bottom:10px solid linear-gradient(#FF4000, transparent); height:20px; width:auto; background:#ccc; } <div></div> ...

What is the process behind Twitter's ability to quickly show my profile?

Scenario I was intrigued by the different loading times of Twitter's profile page based on how it is accessed: Clicking the profile link in the menu results in a 4-second load time with DOM and latest tweets loade ...

Items positioned to the left will not overlap

Having trouble with floating elements? Need to ensure that box 3 aligns under box 1 when resizing the browser window? Use this HTML template: <div class="box"> <p>box 1</p> <p>box 1</p> <p>box 1</p> & ...

Permit the use of the "&" character in mailto href links

When including an email mailto href link and using a & character in the subject, it can cause issues with code rendering. For example, if the subject is "Oil & Gas," only "Oil" may show up. In most cases, you could simply replace the & with th ...

Unable to retrieve all form properties when using enctype='multipart/form-data'

Recently, my NodeJS server has been successfully uploading files to Amazon using a secret form. However, in an effort to enhance security measures, I decided to introduce a password field to the form. Unfortunately, upon doing so, I encountered an issue wh ...

What causes the disparity in the functionality of getServerSideProps between index.js and [id].js in NextJS?

Exploring NextJS and React as part of my e-commerce site development journey has been exciting. However, I encountered a roadblock when trying to connect to an external database that requires real-time updates, making getStaticProps unsuitable for my needs ...

Utilizing jQuery Ajax to retrieve a multitude of data

Utilizing jQuery AJAX to retrieve a string from PHP containing JavaScript, PHP, and HTML has been successful for me using the code below: header("Content-Type: text/html"); echo $content; $.ajax({ type: 'POST', url: url, data: data, ...

The API response was blocked due to the CORS header "Access-control-allow-origin."

I have an index.html file that is used for making HTML and Ajax calls. When I run the file through localhost, the API works perfectly. However, if I directly open the index.html file in Google Chrome or Mozilla Firefox, I encounter a CORS (Cross-Origin Re ...

How can you make the contents of a <DIV> expand?

Picture a scenario where I have a navigation bar in the header of my web page, consisting of several links. What I want is for these links to expand horizontally to fill the parent container without me having to hard-code based on the number of links. In o ...

Issue with AngularJS ui-router failing to resolve a service call

I am facing an issue while trying to implement the resolve feature in my ui-router state provider. Here is how I have configured it: app.config(['$stateProvider', '$urlRouterProvider', '$locationProvider', function ($stat ...

Encountering difficulties while using JavaScript to complete a form due to issues with loading the DOM

Currently, I am attempting to automate the completion of a multi-page form using JavaScript. Below is the script that I am utilizing: // Page 1 document.getElementById("NextButton").click(); // Page 2 completion(document.getElementById("Rec ...

Implementing a PHP back button to navigate through previous pages

Currently, I am in the process of coding a project. In this project, there are a total of 10 pages, each equipped with back and forward buttons. However, when I attempt to navigate using either javascript:history.go(-1) or $url = htmlspecialchars($_SERVER ...