0

I have two select elements. I tried to clean up my old code by making some variables global.

before I had something like this:

var user = $('#my_SiteUsers').val();

Problem was that now I could only get the value from this variable, at other times I may need something like the innerHTML and I'd rather not have to create a new variable for this.

So I created a global variable called user.

initiated at document.ready.But now my variable user and group aren't giving me the values I need. For example, the alerts are being triggers because default option isn't being picked up. Sorry if the question isn't clear. I can post old code if that helps.

$(document).ready(function(){ 
    user = $('#my_SiteUsers');
    group = $('#my_SiteGroups');
    groupsAssigned = $("#my_SPGroupsAssigned");
    groupAvailable = $("#my_SPGroupsAvailable");
    userAssigned = $("#my_SPUsersAssigned").html("");
    userAvailable = $("#my_SPUsersAvailable").html("");

    $("button").click(function() { return false; });

    populate();
});


function populate() {
  //Populate the user list
  strHTMLSiteUsers = "";
  $().SPServices({
      operation: "GetUserCollectionFromSite",
      async: true,
      completefunc: function(xData, Status) {
        $(xData.responseXML).find("User").each(function() {
          strHTMLSiteUsers += "<option value='" + $(this).attr("LoginName") + "'>" + $(this).attr("Name") + "</option>";
        });
        user.append(strHTMLSiteUsers);
      }
  });

  if (user.val() == "default"){
    //don't do anything
  }else{
    //if a user is selected, run 
    RefreshGroupLists();
  }

  //Populate the group list  
  strHTMLSiteGroups = "";
  $().SPServices({
      operation: "GetGroupCollectionFromSite",
      async: true,
      completefunc: function(xData, Status) {
        $(xData.responseXML).find("Group").each(function() {
        strHTMLSiteGroups += "<option value='" + $(this).attr("Name") + "'>" + $(this).attr("Name") + "</option>";          
        });
        group.append(strHTMLSiteGroups);
      }
  });
  if (group.val()=="default"){
    //don't do anything
  }else{
    //if a user is selected, run 
  RefreshUserLists();
  } 
}

JSFiddle: http://jsfiddle.net/JevpS/

Batman
  • 5,563
  • 18
  • 79
  • 155

3 Answers3

2

The code which calls user.val() (and group.val()) is executed before respective completefunc handlers due asynchronous nature of $().SPServices calls. You can only expect select elements to be filled with options inside (or after - in chronological order of execution) completefunc code.

Igor
  • 15,833
  • 1
  • 27
  • 32
  • should I set async to false? – Batman Oct 01 '13 at 04:07
  • It is considered undesirable, since the browser will freeze until the synchronous ajax call returns. Usual practice is to put code that depends on the outcome of asynchronous call in `complete` or `success` handler. – Igor Oct 01 '13 at 04:13
  • Oh okay, so inside the "completefunc", alright, well I removed the refresh lists that ran on document.ready so that issue is solved. Thank you. – Batman Oct 01 '13 at 04:18
0

Try using

var gl_var="";

or

gl_var = undefined

Refer http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html

Also Have a look at this Difference between variable declaration syntaxes in Javascript (including global variables)?

Community
  • 1
  • 1
Vaibs_Cool
  • 6,126
  • 5
  • 28
  • 61
  • i have this line at the very top: var user, group, strHTMLSiteUsers, strHTMLSiteGroups, strHTMLAvailable, strHTMLAssigned, arrOptionsAssigned, arrGroups, arrUsers, intOpts, booMatch, booErr; – Batman Oct 01 '13 at 03:24
  • can u edit your question with the way you have global variable ?? – Vaibs_Cool Oct 01 '13 at 03:29
  • I added a js fiddle with full script – Batman Oct 01 '13 at 03:31
  • I get the same error. It's fine though, I just realized I don't need to run the refresh function onload to begin with, only on change. On change it works fine. I just got rid of the if statements. – Batman Oct 01 '13 at 03:58
  • 1
    @vaibcool: op asked for accesing global variable, nt for initializing. Ntw nice links u provided – Satinder singh Oct 01 '13 at 04:13
0

Declare your variable above the ready(); and define them inside the ready();

Flexo
  • 87,323
  • 22
  • 191
  • 272
UserOnE
  • 29
  • 6