-2

I am trying to implement a simple datepicker in jQuery but I am getting an error like 'datepicker not a function'. Really donot understand why and not able to know where I made the mistake. I have also added jQuery in the bundles and the bundle script is rendered in _Layout page and the script is added in the view but no luck. Could anybody help on what the problem is.

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Reports</title>
<link href="~/Content/Site.css" rel="stylesheet" />
<link href="~/Content/themes/base/datepicker.css" rel="stylesheet" />
<link href="~/Content/themes/ui-lightness/jquery-ui.ui-lightness.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.12.4.js"></script>
<script src="~/Scripts/jquery-ui-1.12.1.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $(function () {
            var j = $.noConflict(true);
            $("#fromdate").datepicker();
        });
    });
    </script>
</head>
<body>
<div class="container">
    <!-- Form Starts-->
    <form name="PTTReport" style="padding-top:20px">
        <div class="panel panel-primary">
            <div style="padding-bottom:25px" class="panel-heading fixed_panel">
                    <span>Report Filters</span>
            </div>
            <div class="panel-body">
                <div class="row">
                    @*From*@
                    <label class="col-sm-1">From</label>
                    <div class="col-sm-2">
                        <div class="input-group">
                            <input type="text" class="form-control" id="fromdate">
                            <span class="input-group-btn">
                                <button class="btn btn-default" type="button">
                                    <span class="glyphicon glyphicon-calendar"></span>
                                </button>
                            </span>
                        </div><!-- /input-group -->
                    </div>

                    @*To*@
                    <label class="col-sm-1">To</label>
                    <div class="col-sm-2">
                        <div class="input-group">
                            <input type="text" class="form-control">
                            <span class="input-group-btn">
                                <button class="btn btn-default" type="button">
                                    <span class="glyphicon glyphicon-calendar"></span>
                                </button>
                            </span>
                        </div><!-- /input-group -->
                    </div>
                </div>
                <!--Submit Button-->
                <div class="col-sm-10"></div>
                <div class="col-sm-2">
                    <button type="submit" class="btn btn-primary text-center">View Projects</button>
                </div>
            </div>
        </div>
ɢʀᴜɴᴛ
  • 32,025
  • 15
  • 116
  • 110
Aneez
  • 61
  • 3
  • 13
  • Why inserting this : `~/` ? Try absolute or relative insertion. See [this question](http://stackoverflow.com/questions/13539705/using-tilde-in-script-tag-src-attribute) – w3spi May 21 '17 at 14:20
  • Why are you calling `noConflict()` inside `document.ready`? Idea doesn't make sense. – charlietfl May 21 '17 at 14:23

3 Answers3

1

Console error clearly shows that it cant find file paths.

try this:

jQuery .css and .js files source

<link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
     rel = "stylesheet">
  <script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
  <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

JS

<script>
     $(function() {
        $( "#datepicker" ).datepicker();
     });
</script>

Body of HTML

<body> 
  <p>Enter Date: <input type = "text" id = "datepicker"></p>
</body>
Abhishek Chandran
  • 1,536
  • 1
  • 13
  • 21
0

try the following:

var $j = jQuery.noConflict();
$j("#fromdate").datepicker();
amhev
  • 313
  • 1
  • 3
  • 12
0

Always check your browser console and network calls to make certain its loading all required files or not first.
$.datepicker() is not a function. It clearly shows that it can't recognize the base script where this function is defined. Using ~ to define relative paths certainly is not loading your jquery/jquery-ui scripts. If you want to make sure you can check this code here and it works!

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Reports</title>
  <script
  src="https://code.jquery.com/jquery-2.2.4.min.js"
  integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
  crossorigin="anonymous"></script>
  <script
  src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"
  integrity="sha256-xNjb53/rY+WmG+4L6tTl9m6PpqknWZvRt0rO1SRnJzw="
  crossorigin="anonymous"></script>
<script>

    $(document).ready(function () {
        $(function () {
            //var j = $.noConflict(true);
            $("#fromdate,#todate").datepicker();
        });
    });
    </script>
</head>
<body>
<div class="container">
    <!-- Form Starts-->
    <form name="PTTReport" style="padding-top:20px">
        <div class="panel panel-primary">
            <div style="padding-bottom:25px" class="panel-heading fixed_panel">
                    <span>Report Filters</span>
            </div>
            <div class="panel-body">
                <div class="row">
                    @*From*@
                    <label class="col-sm-1">From</label>
                    <div class="col-sm-2">
                        <div class="input-group">
                            <input type="text" class="form-control" id="fromdate">
                            <span class="input-group-btn">
                                <button class="btn btn-default" type="button">
                                    <span class="glyphicon glyphicon-calendar"></span>
                                </button>
                            </span>
                        </div><!-- /input-group -->
                    </div>

                    @*To*@
                    <label class="col-sm-1">To</label>
                    <div class="col-sm-2">
                        <div class="input-group">
                            <input type="text" class="form-control" id="todate">
                            <span class="input-group-btn">
                                <button class="btn btn-default" type="button">
                                    <span class="glyphicon glyphicon-calendar"></span>
                                </button>
                            </span>
                        </div><!-- /input-group -->
                    </div>
                </div>
                <!--Submit Button-->
                <div class="col-sm-10"></div>
                <div class="col-sm-2">
                    <button type="submit" class="btn btn-primary text-center">View Projects</button>
                </div>
            </div>
        </div>

I added jquery and jquery-ui from cdn and are working fine, make sure you added your jquery-ui script tag after jquery. Updated JS Bin is here.

What to do next

Use '/' as relative path for linking your css and scripts that will solve your problem.

breakit
  • 356
  • 2
  • 8