0

I created a object array inside a click function like this:

    $(".tableButton").click(function()
    {
         CK.tableArray = ["test1","test2","test3","test3"]; // this works
         processTableInfo(CK.tableArray); // this works
    });

Now I need to access the CK.tableArray from another part of the application using the same array information from click function above. I need to access a different range of elements inside this array from above...

    $(".displayTable").click(function()
    {
        alert(CK.tableArray) // not define
    })
Yetimwork Beyene
  • 239
  • 1
  • 11

2 Answers2

2

One way is to park the variable underneath a global namespace, such as window.

$(".tableButton").click(function()
{
     window.tableArray = ["test1","test2","test3","test3"]; // this works
     processTableInfo(window.tableArray); // this works
});

Then access it through the global namespace:

$(".displayTable").click(function()
{
    alert(window.tableArray) // not define
})

In your example posted, it really depends on where CK is defined (the scope), however. For more information on this, you can visit the MDN article on Functions and function scope.

You can also checkout the MDN article Scope Cheatsheet

You can also checkout the following StackOverflow post

Community
  • 1
  • 1
obimod
  • 797
  • 10
  • 26
2

If your aim is to pass around array object only, try tacking it with window object.

window.tableArray = ["test1","test2",...]
TheVillageIdiot
  • 40,053
  • 20
  • 133
  • 188