1

I have this code that got me stuck for days I don't understand why does it say in my console that my function is not defined here's the code. by the way, i am super new to jQuery I really don't have much knowledge in it so any help? can somebody tell me where I went wrong that it's giving me this error

(ReferenceError: createGraph is not defined)

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>

        <script src="bin/js/raphael.js"></script>
        <script src="bin/js/popup.js"></script>
        <script src="bin/js/analytics.js"></script>
</head>
<body>

 <div class="action-button">
    <button id="1" value="1074">Button 1</button>
    <button id="2" value="1074">Button 2</button>
 </div>

  <div id="output"></div>

<script>
/////////////////////////////////////////////////////
// this script will check if the document is ready and will display data for button 1
     $(document).ready(function () {
    $("#1").trigger("click");
    });

    ///////////////////////////////////////////////////////////////////////////////////////
        $("button").click(function () {
          var attr = $(this).attr("id");
          var val = $(this).val();

          $.ajax({
          type: "POST",
          url: "bin/some.php",
          data: { lookbookID: val, type:attr  }
    }).done(function( html ) {
      $("#output").html(attr + ' - ' +  val + ' - ' + html );
        createGraph();

    });
});
</script>
</body>
</html>

and the JS code

window.onload = function () {
    function getAnchors(p1x, p1y, p2x, p2y, p3x, p3y) {
        var l1 = (p2x - p1x) / 2,
            l2 = (p3x - p2x) / 2,
            a = Math.atan((p2x - p1x) / Math.abs(p2y - p1y)),
            b = Math.atan((p3x - p2x) / Math.abs(p2y - p3y));
        a = p1y < p2y ? Math.PI - a : a;
        b = p3y < p2y ? Math.PI - b : b;
        var alpha = Math.PI / 2 - ((a + b) % (Math.PI * 2)) / 2,
            dx1 = l1 * Math.sin(alpha + a),
            dy1 = l1 * Math.cos(alpha + a),
            dx2 = l2 * Math.sin(alpha + b),
            dy2 = l2 * Math.cos(alpha + b);
        return {
            x1: p2x - dx1,
            y1: p2y + dy1,
            x2: p2x + dx2,
            y2: p2y + dy2
    };
}

    function createGraph() {
       // Grab the data
       alert("i made it!");
        var labels = [],
            data = [];
        $("#data tfoot th").each(function () {
        labels.push($(this).html());
        });
        $("#data tbody td").each(function () {
            data.push($(this).html());
        });

     }
    };

I updated this sorry I excluded that window.onload I still get that reference error but if I put the function outside window.onload = function then my function would work is there any way I can access my function? I don't know what will happen if I remove the said code from windows.onload I need help again :)

the link for the function just in case it would be of any help https://github.com/ksylvest/raphael-analytics

4
  • 2
    That's not how functions are defined. You need to learn basic Javascript syntax. Commented May 10, 2013 at 17:06
  • 1
    $(function createGraph()() { is a pretty blatant syntax error. Commented May 10, 2013 at 17:07
  • +1 Just fix the typo error. Commented May 10, 2013 at 17:08
  • thanks guys i didn't realize i had that $() lol epic fail anyway thank you very much! Commented May 10, 2013 at 17:11

2 Answers 2

2
function createGraph() {
// Grab the data
    var labels = [],
        data = [];
    $("#data tfoot th").each(function () {
        labels.push($(this).html());
    });
    $("#data tbody td").each(function () {
        data.push($(this).html());
    });

   //code here i erased so it wont be too long and i just added this function so i could call it anyway this code is actually the code that creates the graph in raphael JS
 }
Sign up to request clarification or add additional context in comments.

7 Comments

You've got all kinds of dangling braces and parenthesis.
Thanks bro this one helped a lot
make a new question if it's not related to this specific problem
well it's the same i still get that reference error anyway i updated the JS above can you check if that's a good idea? thanks
try to wrap it in $(document).ready(function(){ // YOURCODE });
|
1

createGraph is not a global function , Because $() create a new context,createGraph became a local variables,so if you try to call it ,you get an error.

so let it out from $().

Code like Upstairs..

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.