Note that there are some explanatory texts on larger screens.

plurals
  1. POAccess javascript objects that are having same name
    primarykey
    data
    text
    <p>I have a groups user control and javascript is defined inside the control. In a page, I have two such controls, which work independently. Rendered javascript is something similar to below:</p> <pre><code>//control 1 var Groups = (function () { var obj = {}; obj.selectedid = 2; return obj; }()); //control 2 var Groups = (function () { var obj = {}; obj.selectedid = 2; //it is different here, to keep it simple i just hardcoded as '2' return obj; }()); </code></pre> <p>Could anyone please tell me how to access 1st Groups variable (control) on the Page. I thought something like Groups[0] would give me the result but it's not.</p> <hr> <p>Thanks for your replies. Much Appreciated. I am adding more information to be clear. User control contains dual listbox, user can add/remove values from list1 to list2. So I had encapsulated all this logic as below. </p> <pre><code> var Groups = (function () { var obj = {}; obj.selectedid = 2; //some local functions for internal operations such as function moveGroups(source, target){ } //there are public functions to initialize this control or to add groups as below // Groups can be added from external page by calling Groups.AddGroups(data); obj.AddGroups = function(data) { //refers to local variables and functions and adds data to listboxes }; return obj; }()); </code></pre> <p>Problem is I have two such Groups controls on the page. Group1 contains its own dual list and similarly Group2. Now, I would need to access AddGroups function of both objects work independently like Groups1.AddGroups(data) or Groups2.AddGroups(data) from the page. </p> <hr> <p>I have worked out with below solution.</p> <pre><code>var Groups = Groups || []; //Check object already exists Groups.push( //kept all the existing code here... (function () { var obj = {}; obj.selectedid = 2; //some local functions for internal operations such as function moveGroups(source, target){ } //there are public functions to initialize this control or to add groups as below // Groups can be added from external page by calling Groups.AddGroups(data); obj.AddGroups = function(data) { //refers to local variables and functions and adds data to listboxes }; return obj; }()) ); </code></pre> <p>Now in my page, I refer using:</p> <pre><code>Groups[0].AddAvailGroups(); Groups[1].AddAvailGroups(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload