Pakk
Pakk

Reputation: 1339

jQuery undefined after $(function() {

I can't seem to pinpoint why this is failing - any ideas? -> See within .aspx

custom.master references / and script

<link rel="stylesheet" href="/admin/css/custom.css" />
<script src="/admin/js/jquery-1.9.1.min.js"></script>
<script src="/admin/js/jquery-1.10.2-ui.min.js"></script>
<link href="css/ui-lightness/jquery-ui-1.8.13.custom.css" rel="stylesheet"/>
<asp:ContentPlaceHolder ID="HeadPlaceHolder" runat="server"></asp:ContentPlaceHolder>

<script>
    $(function () {
        var $ = jQuery.noConflict(); <-- This here is the culprit

Within rPage.aspx (MasterPageFile="~/admin/custom.master")

<asp:Content ID="Content1" ContentPlaceHolderID="HeadPlaceHolder" runat="server">
    <link href="css/bootstrap-3.3.5.min.css" rel="stylesheet" />
</asp:Content>

<script>
    $(function () {
        var $ = jQuery.noConflict(); //**Without this line the next line fails**
        $("#<%= txtStartDate.ClientID %>").datepicker();

Error:

from console: TypeError: $ is not a function rPage:380:13

from debugger firefox dev edition:

378 $(function () {
379        //var $ = jQuery.noConflict();
380        $("#ContentPlaceHolder_txtStartDate").datepicker();

Upvotes: 1

Views: 320

Answers (1)

Prisoner ZERO
Prisoner ZERO

Reputation: 14166

Looking at the (currently) posted example, I see no reason for the NoConflict call. I am assuming it is because maybe the other library isn't (yet) shown in your sample code.

That said...

Try moving your NoConflict declaration outside of your Immediately-Invoked Function Expression (IIFE) & right BEFORE you bring-in your other library..

Like so...

<script>
$.noConflict();
// Code that uses other library's $ can follow here.

$(function () {
     // Do awesome stuff here...
})();
</script>

I also suggest...

Aliasing jQuery into your IIFE's like so...

<script>
    $(function ($) {
         // Do awesome stuff here...
    })(jQuery);
</script>

Upvotes: 2

Related Questions