0

I have a problem with my JavaScript, since I am trying to call a function but my web console tells me that is not defined. The function is in a seperate .js document where there are other JavaScript function which I am able to call without problems. It is only this first one.

This is my html:

<div class="col-lg-8">
            <div class="panel-style space table_space">
                <h3 class="heading-title">Klienter</h3>
                <div class="table-responsive">
                    <table class="table simple">
                        <thead>
                            <tr>
                                <th>CVR</th>
                                <th>Firmanavn</th>
                                <th>By</th>
                                <th>Kontaktperson</th>
                                <th>Direkte telefon</th>
                            </tr>
                        </thead>
                        <tbody id="list_all_clients">
                            <script type="text/javascript">list_all_clients()</script>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

This is the top of the HTML where I am including the functions:

    <!DOCTYPE html>
<html lang="da">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">


    <title></title>

    <script src="scripts/jquery-1.11.0.min.js"></script>
    <script src="scripts/jquery-ui.min.js"></script>

    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->

    <script type='text/javascript' src='scripts/jquery.prettycheckable/prettycheckable.js'></script>

    <script src="scripts/jquery.scrollbar/jquery.mousewheel.js"></script>

    <script src="scripts/jquery.scrollbar/perfect-scrollbar.js"></script>

    <script type="text/javascript" src="scripts/jquery.tipsy/tipsy.js"></script>

    <script type="text/javascript" src="scripts/jquery.easypiechart/jquery.easypiechart.js"></script>

    <script type="text/javascript" src="scripts/jquery.gmap/gmap3.js"></script>
    <script type="text/javascript" src="js/api_data.js"></script>

    <!--
    <script src="scripts/jquery.charts/morris.js"></script>

    <script src="//cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>
    <script src="http://cdn.oesmith.co.uk/morris-0.4.3.min.js"></script>
    -->

    <!--
    <script type="text/javascript" src="scripts/jquery.nvdcharts/d3.v3.js"></script>
    <script type="text/javascript" src="scripts/jquery.nvdcharts/nvd.js"></script>
    -->

This is my JavaScript document:

function list_all_clients(){

    var datastring = "get_all_clients=yes"

    $.ajax({
        type : "GET",
        url : "api.php",
        data : datastring,
        dataType : "html",
        success : function(data) {
            $('#list_all_clients').html(data);
        }
    });
}

function add_client(){
        var cvr = $("input#cvr").val();
        var firmanavn = $("input#firmanavn").val();
        var hoved_tlf = $("input#hoved_tlf").val();
        var geo_omr = $("input#geo_omr").val();
        var vej = $("input#vej").val();
        var husnummer = $("input#husnummer").val();
        var by = $("input#by").val();
        var postnummer = $("input#postnummer").val();
        var land = $("input#land").val();
        var branche = $("input#branche").val();
        var ansatte = $("input#ansatte").val();
        var revenue = $("input#revenue").val();
        var bruttofortjeneste = $("input#bruttofortjeneste").val();
        var k_navn = $("input#k_navn").val();
        var k_stilling = $("input#k_stilling").val();
        var direkte_tlf = $("input#direkte_tlf").val();
        var direkte_email = $("input#direkte_email").val();

        var datastring = 'new_client=yes'+'&cvr='+cvr+'&firmanavn='+firmanavn+'&hoved_tlf='+hoved_tlf+'&geo_omr='+geo_omr+'+'&vej='+vej+'&husnummer='+husnummer+'&by='+by+'&postnummer='+postnummer+'&land='+land+'&branche='+branche+
        '&ansatte='+ansatte+'&revenue='+revenue+'&bruttofortjeneste='+bruttofortjeneste+'&k_navn='+k_navn+'&k_stilling='+k_stilling+'&direkte_tlf='+direkte_tlf+'&direkte_email='+direkte_email;

        $.ajax({
            type : "GET",
            url : "api.php",
            data : datastring,
            dataType : "html",
            success : function(data) {
                get_all_clients();
            }
    });

}
5
  • I think you forgot ' or extra ' in datastring variable. please check it. Commented Feb 13, 2015 at 11:23
  • "Placed at the end of the document so the pages load faster", --> you're calling list_all_clients() in an inline script before it was loaded. Put the script tag loading the script containing list_all_clients() before line <script type="text/javascript">list_all_clients()</script>. Commented Feb 13, 2015 at 11:24
  • Putting it just before </body> did not work. Not sure what you mean about 'or extra'? Commented Feb 13, 2015 at 11:28
  • 1
    You've an inline script calling a function, if the script containing that function is loaded after the inline script calls that function, what you can expect? Also please tell exactly which function is not defined. Commented Feb 13, 2015 at 11:31
  • Put your external Js file on root level. Commented Feb 13, 2015 at 11:32

1 Answer 1

2

Typo:

geo_omr+'+'&vej='

replace that with:

geo_omr+'&vej='

Also, make sure you've loaded the script that contains list_all_clients() in the page's <head>, so the function exists before you're trying to call it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.