3

Hi am getting an uncought referecne error when adding a custom JS in my theme. On the console I am getting this error-

pricing.js:122 Uncaught ReferenceError: $ is not defined
at pricing.js:122

My Pricing.js is an additional Js file, which is causing the error. Please suggest how to resolve this error.

My pricing js is attached herewith, pricing.js

pricing.js

define('js/theme',['jquery', 'domReady!'], function(jQuery){
    function ajaxCall() {
        this.send = function(data, url, method, success, type) {
          type = type||'json';
          var successRes = function(data) {
              success(data);
          };

          var errorRes = function(e) {
              console.log(e);
              alert("Error found \nError Code: "+e.status+" \nError Message: "+e.statusText);
          };
            $.ajax({
                url: url,
                type: method,
                data: data,
                success: successRes,
                error: errorRes,
                dataType: type,
                timeout: 60000
            });

          }

        }

function pricingInfo() {
  //  var rootUrl = "location-api/api.php";
    var call = new ajaxCall();
    this.getManufacturing = function(id) {
        $(".manufacturing option:gt(0)").remove();
        var url = rootUrl+'?type=getManufacturing&brandNameId=' + id;
        var method = "post";
        var data = {};
        $('.manufacturing').find("option:eq(0)").html("Please wait..");
        call.send(data, url, method, function(data) {
            $('.manufacturing').find("option:eq(0)").html("Select Manufacturing Year");
            if(data.tp == 1){
                $.each(data['result'], function(key, val) {
                    var option = $('<option />');
                    option.attr('value', key).text(val);
                    $('.manufacturing').append(option);
                });
                $(".manufacturing").prop("disabled",false);
            }
            else{
                 alert(data.msg);
            }
        });
    };

    this.getCondition = function(id) {
        $(".condition option:gt(0)").remove(); 

        var url = rootUrl+'?type=getCondition&manufacturingId=' + id;
        var method = "post";
        var data = {};
        $('.condition').find("option:eq(0)").html("Please wait..");
        call.send(data, url, method, function(data) {
            $('.condition').find("option:eq(0)").html("Select Condition");
            if(data.tp == 1){
                $.each(data['result'], function(key, val) {
                    var option = $('<option />');
                    option.attr('value', key).text(val);
                    $('.condition').append(option);
                });
                $(".condition").prop("disabled",false);
            }
            else{
                alert(data.msg);
            }
        }); 
    };
    this.getBrandName = function(id) {
        $(".brandName option:gt(0)").remove(); 
        $(".manufacturing option:gt(0)").remove(); 
        var url = rootUrl+'?type=getBrandName&producttypeId=' + id;
        var method = "post";
        var data = {};
        $('.brandName').find("option:eq(0)").html("Please wait..");
        call.send(data, url, method, function(data) {
            $('.brandName').find("option:eq(0)").html("Select Brand Name");
            if(data.tp == 1){
                $.each(data['result'], function(key, val) {
                    var option = $('<option />');
                    option.attr('value', key).text(val);
                    $('.brandName').append(option);
                });
                $(".brandName").prop("disabled",false);
            }
            else{
                alert(data.msg);
            }
        }); 
    };

    this.getProducttype = function() {
        var url = rootUrl+'?type=getProducttype';
        var method = "post";
        var data = {};
        $('.producttype').find("option:eq(0)").html("Please wait..");
        call.send(data, url, method, function(data) {
            $('.producttype').find("option:eq(0)").html("Select Product Type");
            console.log(data);
            if(data.tp == 1){
                $.each(data['result'], function(key, val) {
                    var option = $('<option />');
                    // console.log(key); console.log(" ");console.log(val+" <br>");
                    option.attr('value', key).text(val);
                    $('.producttype').append(option);
                });
                $(".producttype").prop("disabled",false);
            }
            else{
                alert(data.msg);
            }
        }); 
    };

}

$(function() {
var loc = new pricingInfo();
loc.getProducttype();
 $(".producttype").on("change", function(ev) {
        var producttypeId = $(this).val();
        if(producttypeId != ''){
        loc.getBrandName(producttypeId);
        }
        else{
            $(".brandName option:gt(0)").remove();
        }
    });

 $(".brandName").on("change", function(ev) {
        var brandNameId = $(this).val();

        if(brandNameId != ''){
        loc.getManufacturing(brandNameId);
        }
        else{
            $(".manufacturing option:gt(0)").remove();
        }
    });

     $(".manufacturing").on("change", function(ev) {
        var manufacturingId = $(this).val();
        if(manufacturingId != ''){
        loc.getCondition(manufacturingId);
        }
        else{
            $(".condition option:gt(0)").remove();
        }
    });


});

 $('#calculateprice').on('click', function () {
            var manufacturingtext=$('.manufacturing  option:selected').text();
            var conditiontext=$('.condition  option:selected').text();
            var brandNametext=$('.brandName  option:selected').text();
            var producttypetext=$('.producttype  option:selected').text();
            var yourPrice=$(".yourprice").val();
            var email=$("#email").val();
            var mobile=$("#mobile").val();

            var url = calculatetUrl+'?calculate=getManufacturing&brandName=' + brandNametext+'&productType='+producttypetext+'&condition='+ conditiontext +'&manufacturingYr=' +manufacturingtext+'&assetPrice='+yourPrice+'&email='+email+'&mobile='+mobile;
            var call = new ajaxCall();
            var method = "post";
            var data = {};
            var pattern = /^\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b$/i

                if(!pattern.test(email))
                {
                  alert('Please provide a valid e-mail address');
                }
                else{
             $('#result').html("Please wait..");
            call.send(data, url, method, function(data) {

            if(data.tp == 1){
                $("#result").html("<span>"+data['result']+" </span>");
              //  $(".manufacturing").prop("disabled",false);
            }
            else{
                 alert(data.msg);
            }
        });
                }
   });



    $(document).on('change', '#yourprice', function(){
    $price=$("#yourprice").val();
        if($price>0 || $price!='')
            {
            $("#dynamicview").show();
            $("#calculateprice").show();

            }
        else{
            $("#dynamicview").hide();
            $("#calculateprice").hide();

        }


    });
});
  • on line 122 change line to this $(document).ready(function(){ – fmsthird Apr 10 at 11:57
0

Add require in your js file

require([
    'jquery',
    "jquery/ui"
], function($){
    'use strict';
    //Your Js code here
});

And use jQuery instead of $ to avoid conflicts

0

Use full jQuery document ready event

$(document).ready(function()

Try this one:

define('js/theme',['jquery', 'domReady!'], function(jQuery){
    function ajaxCall() {
        this.send = function(data, url, method, success, type) {
          type = type||'json';
          var successRes = function(data) {
              success(data);
          };

          var errorRes = function(e) {
              console.log(e);
              alert("Error found \nError Code: "+e.status+" \nError Message: "+e.statusText);
          };
            $.ajax({
                url: url,
                type: method,
                data: data,
                success: successRes,
                error: errorRes,
                dataType: type,
                timeout: 60000
            });

          }

        }

function pricingInfo() {
  //  var rootUrl = "location-api/api.php";
    var call = new ajaxCall();
    this.getManufacturing = function(id) {
        $(".manufacturing option:gt(0)").remove();
        var url = rootUrl+'?type=getManufacturing&brandNameId=' + id;
        var method = "post";
        var data = {};
        $('.manufacturing').find("option:eq(0)").html("Please wait..");
        call.send(data, url, method, function(data) {
            $('.manufacturing').find("option:eq(0)").html("Select Manufacturing Year");
            if(data.tp == 1){
                $.each(data['result'], function(key, val) {
                    var option = $('<option />');
                    option.attr('value', key).text(val);
                    $('.manufacturing').append(option);
                });
                $(".manufacturing").prop("disabled",false);
            }
            else{
                 alert(data.msg);
            }
        });
    };

    this.getCondition = function(id) {
        $(".condition option:gt(0)").remove(); 

        var url = rootUrl+'?type=getCondition&manufacturingId=' + id;
        var method = "post";
        var data = {};
        $('.condition').find("option:eq(0)").html("Please wait..");
        call.send(data, url, method, function(data) {
            $('.condition').find("option:eq(0)").html("Select Condition");
            if(data.tp == 1){
                $.each(data['result'], function(key, val) {
                    var option = $('<option />');
                    option.attr('value', key).text(val);
                    $('.condition').append(option);
                });
                $(".condition").prop("disabled",false);
            }
            else{
                alert(data.msg);
            }
        }); 
    };
    this.getBrandName = function(id) {
        $(".brandName option:gt(0)").remove(); 
        $(".manufacturing option:gt(0)").remove(); 
        var url = rootUrl+'?type=getBrandName&producttypeId=' + id;
        var method = "post";
        var data = {};
        $('.brandName').find("option:eq(0)").html("Please wait..");
        call.send(data, url, method, function(data) {
            $('.brandName').find("option:eq(0)").html("Select Brand Name");
            if(data.tp == 1){
                $.each(data['result'], function(key, val) {
                    var option = $('<option />');
                    option.attr('value', key).text(val);
                    $('.brandName').append(option);
                });
                $(".brandName").prop("disabled",false);
            }
            else{
                alert(data.msg);
            }
        }); 
    };

    this.getProducttype = function() {
        var url = rootUrl+'?type=getProducttype';
        var method = "post";
        var data = {};
        $('.producttype').find("option:eq(0)").html("Please wait..");
        call.send(data, url, method, function(data) {
            $('.producttype').find("option:eq(0)").html("Select Product Type");
            console.log(data);
            if(data.tp == 1){
                $.each(data['result'], function(key, val) {
                    var option = $('<option />');
                    // console.log(key); console.log(" ");console.log(val+" <br>");
                    option.attr('value', key).text(val);
                    $('.producttype').append(option);
                });
                $(".producttype").prop("disabled",false);
            }
            else{
                alert(data.msg);
            }
        }); 
    };

}

$(document).ready(function(){
var loc = new pricingInfo();
loc.getProducttype();
 $(".producttype").on("change", function(ev) {
        var producttypeId = $(this).val();
        if(producttypeId != ''){
        loc.getBrandName(producttypeId);
        }
        else{
            $(".brandName option:gt(0)").remove();
        }
    });

 $(".brandName").on("change", function(ev) {
        var brandNameId = $(this).val();

        if(brandNameId != ''){
        loc.getManufacturing(brandNameId);
        }
        else{
            $(".manufacturing option:gt(0)").remove();
        }
    });

     $(".manufacturing").on("change", function(ev) {
        var manufacturingId = $(this).val();
        if(manufacturingId != ''){
        loc.getCondition(manufacturingId);
        }
        else{
            $(".condition option:gt(0)").remove();
        }
    });


});

 $('#calculateprice').on('click', function () {
            var manufacturingtext=$('.manufacturing  option:selected').text();
            var conditiontext=$('.condition  option:selected').text();
            var brandNametext=$('.brandName  option:selected').text();
            var producttypetext=$('.producttype  option:selected').text();
            var yourPrice=$(".yourprice").val();
            var email=$("#email").val();
            var mobile=$("#mobile").val();

            var url = calculatetUrl+'?calculate=getManufacturing&brandName=' + brandNametext+'&productType='+producttypetext+'&condition='+ conditiontext +'&manufacturingYr=' +manufacturingtext+'&assetPrice='+yourPrice+'&email='+email+'&mobile='+mobile;
            var call = new ajaxCall();
            var method = "post";
            var data = {};
            var pattern = /^\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b$/i

                if(!pattern.test(email))
                {
                  alert('Please provide a valid e-mail address');
                }
                else{
             $('#result').html("Please wait..");
            call.send(data, url, method, function(data) {

            if(data.tp == 1){
                $("#result").html("<span>"+data['result']+" </span>");
              //  $(".manufacturing").prop("disabled",false);
            }
            else{
                 alert(data.msg);
            }
        });
                }
   });



    $(document).on('change', '#yourprice', function(){
    $price=$("#yourprice").val();
        if($price>0 || $price!='')
            {
            $("#dynamicview").show();
            $("#calculateprice").show();

            }
        else{
            $("#dynamicview").hide();
            $("#calculateprice").hide();

        }


    });
});
0
define([
  "jquery",
  "domReady!",
  "js/theme"
], 
function($) {
    /* Your code here */
});

Try this way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.