How to get global variable in JQuery functions

Posted by Tahir Akram on Stack Overflow See other posts from Stack Overflow or by Tahir Akram
Published on 2010-04-07T06:31:02Z Indexed on 2010/04/07 6:33 UTC
Read the original article Hit count: 351

I want to access my global javascript variable in JQuery methods. But I am unable to get it when I go to attach a click even to a div. As following.

How can I do that? I mean do I need to rely on hidden fields for some state management?

var divCount = 3;
$(function() {
//divCount is accessible here
    $("#sortable").sortable({
        revert: true
    }); 

    $("#new").click(function(){
        if (divCount<7){
                     //divCount is not accessible here. why? and how?
            var thisCount =  ++divCount;    

            $("#draggable_"+thisCount).addClass("draggable");
        }
    });
});

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about jquery-ui