Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am stuck with this error and I find it frustrating because I already used this code in my old project and it's working fine but when I add the code for my new project it display nothing like it's not working and when I look up the browser console it presents the error Uncaught ReferenceError: showDays is not defined. I put this code inside the onchange event of a select control..

Here is the code that generates the error:
JavaScript
function showDays(str)
{
    $.get("<?php echo site_url('wip/get_deliverydays');?>",{q:str},function(data){
        $("#deliverydays").html(data);
    });
}


What I have tried:

I tried searching solution for this error but they gave answers that are not applicable to my problem.
Posted
Updated 22-Feb-16 22:33pm
Comments
Sergey Alexandrovich Kryukov 23-Feb-16 4:21am    
This is not what we mean by "trying". It's not using "trial and error" and mot searching for the solution. People are not publishing solutions of your problem, unless they are quite common. Trying, in the sense implied here, is using your logic and fundamental facts to rationally solve the problem.

In this particular case, in your "old project" which is "working fine", showDays had been defined in the context where you use it, and in your "new" one, it is not. As simple as that. Define it and you are done. Beyond that, the question makes no sense.

—SA

It is mostly scope issue. Although showDays() function defined it is not accessible due to global scope. So you need to put your code outside jQuery function or you need to call the function inside jQuery function scope:

Option 1: Put your custom function outside
JavaScript
function showDays(str)
    $.get("<?php echo site_url('wip/get_deliverydays');??>",{q:str},function(data){
        $("#deliverydays").html(data);
    });
}

$(function () {

});

<div id="divClick" onclick="showDays('value')" >Click Me!</div>

Option 2: Use inline handler and unobtrusive JavaScript

JavaScript
$(function () {
    function showDays(str){
       $.get("<?php echo site_url('wip/get_deliverydays');??>",{q:str},function(data){
         $("#deliverydays").html(data); 
       });
    }

    $('#divClick').click(function(){
        showDays("someValue");
    });
});

HTML element with no inline code:
HTML
<div id="divClick">Click Me!</div>
 
Share this answer
 
v3
Use some elementary logic. Look at your own code sample. It tells you and us that showDays is actually defined. When you face "not defined" error, it's about some fragment of code where the symbol in question is not defined. Therefore, the problem is not in the code you show, but in the code trying to use this function. Therefore, your question does not really make sense: you don't show the source of the problem.

At the same time, it's clear what to recommend you. The problem is really simple.

Find out the code which really cause the exception. Maybe the code you are showing in this page is totally absent; that is, you my show the fragment of code which is not actually used on your page in question. If it is used, look at the fragment of code using the function and try to understand why the function is not accessible in that fragment. If it does not help you much, also look at your "old project" which is "working fine", locate both the function and all places when the function is used, and try to understand why in that old code it works.

And then, look at my comment to the question and make some conclusion. You should not use trial and error approach, but should understand how code really works, at least the code which you right yourself and which is "working fine". Look at that code critically; maybe even that is not really "fine", if you fail to understand how it works.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900