4

I've got a function changeGraph() inside the jQuery-wrapper which I need to call somehow from outside it. I need to access the function setData from the jQuery based graph-library Flot.

The source is looking like this:

function changeGraph(){
    // I need to access $.plot.setData somehow
};  

var d2 = [[0, 0], [20, 300000]];

$(function () {              
    $.plot($("#placeholder"), 
    [{color: "#000000", data: d2}],
    {

    series: {
        lines: { show: true, fill:true, fillColor: {colors: [ "#d1ddea","#8e959d"]}},
        points: { show: false }
          }
       }
    );


});

How can I accomplish this?

6
  • Can you move the function outside of the function scope? Commented Jun 26, 2011 at 20:27
  • I need to access this variable inside the scope $.plot($("#placeholder") , I don't know to do this outside the scope Commented Jun 26, 2011 at 20:29
  • @Hedge - Can you post more of your code? I don't know why you couldn't have the changeGraph() in the global scope. You might consider, if you need to, using jQuery's $.data() to pass data into your function. Commented Jun 26, 2011 at 20:31
  • You can still use $ if you move your function outside, like I showed in my answer. Commented Jun 26, 2011 at 20:31
  • @jtbandes - I suspect he's got other functions in that scope. The OP needs to provide more context for his problem by posting more code, I think. Commented Jun 26, 2011 at 20:34

3 Answers 3

8
var changeGraph;

$(function () {

    changeGraph = function () {
        // Need to access $.plot($("#placeholder") here
    };

});

changeGraph(); // call this when document is ready at least
Sign up to request clarification or add additional context in comments.

2 Comments

This isn't really necessary.. you can access $ from inside changeGraph even if you don't create it within $(function(){...});
@jtbandes, I don't know, maybe we don't know the full context of this particular problem. The question author asked for a way to call a function from outside jQuery closure which is defined in jQuery closure and I just showed how to do this. I don't know the details :)
2

You should move your function outside of the callback function.

2 Comments

how do I access $.plot($("#placeholder") from inside the scope then?
@Hedge - $.plot() is not included in your question. You need to post more of your code (preferably everything inside your function(){}).
2
function changeGraph() {
    // ...
}
$(function() {
    changeGraph();
});

3 Comments

I don't really understand this. The second changeGraph() just executes the function, doesn't it? I need to access another variable from inside the scope.
Which scope? You could use function changeGraph() { $.plot(...); } if you want to. This will still work.
Perhaps you misunderstand the wrapper $(function() { ... });. This just sets up a function to be executed when the document is loaded (or the DOM is ready for manipulation). You can still use $ in other places.

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.