Stack Exchange
Why doesn't '$.each' seem to work?
When I have the following inside the same HTML document it seems to work:
<script type="text/javascript">
datasets[0] = {
    "buildnames": {
        label: "buildnames",
        data: ["test2-a1", "test2-a2", "test2-a3",  "test2-a4","test2-a5", "test2-a6"]
    }
};

function doSomething() {
    var i = 0;
    for (var j = 0; j < datasets.length; j++){
        $.each(datasets[j], function(key, val) {
            val.color = i;
            ++i;
        });

        var choiceContainer = $("#choices");
        //var testkey = new RegExp("cover");
        $.each(datasets[j], function(key, val) {
            if (key.indexOf("failed") > -1 || key.indexOf("cover") > -1) {
                choiceContainer.append('<br /><input type="checkbox" name="' + key +
                '" checked="checked" id="id' + key + '">' +
                '<label for="id' + key + '">'
                + val.label + '</label>');
            }
        });
    }
}
</script>

But when I'm exporting this to a separate JavaScript file and then importing it and call the function like this
<script type="text/javascript" language="Javascript" src="root/include/graph_1.js"></script>

<a href="javascript:doSomething()">doSomething</a>

I get the following error: '$' is undefined  and points at "$.each(.."
So why can't I use this function is I "export" all JavaScript code in an HTML document to another separate JavaScript file?
***********edit***********************************
I tried the tips below  and changed the order
@idbentley I did what you said and changed the order of those 2 so it is
    <script language="javascript" type="text/javascript" src="root/include/jquery.js"></script>
    <script language="javascript" type="text/javascript" src="root/include/jquery.flot.js"></script>
    <script language="javascript" type="text/javascript" src="root/include/jquery.flot.crosshair.js"></script>
    <script type="text/javascript" language="Javascript" src="root/include/graph_1.js"></script>

But now I get another error "Exception thrown and not caught" Anyone know what this error means?

Are you including your jquery script file above the include you have listed?

is your <script /> tag for jquery before or after this?

Start the debugger/firebird/devtools and catch the Exception, this way you can provide us with more details.

Hard to tell with just hwat you gave us, but it sounds like you aren't including jQuery, or that you are loading jQuery after your script.

I did what you said and changed the order of those 2 so it is




You need to include the JQuery library BEFORE you include any of your own scripts.
