jQuery 1.6 attr returns undefined-how can i prevent this

Jun 24, 2011 at 3:10 PM

I just noticed that my update statements were filling my list with Undefined, while investigating I learned that As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set. Does anyone have a recommended approach for how to handle empty fields so that this can be prevented?

I'm populating a variable like this: client = $(this).attr("ows_Client");
and then I'm using UpdateListItems with value pairs like this. 
     $().SPServices({
      operation: "UpdateListItems",
      async: false,
      listName: "Delegations",
      //batch: The batch command to tell UpdateListItems what to do
      batchCmd: "New",
      valuepairs: [["Activity", strActivity],["EndDate", endDate],["StartDate",startDate], ["Delegate", delegateAccount], ["Delegator",delegatorAccount],["Responsibility",respCode],["ItemInBatch",i+1],["TotalInBatch",count],["Client",client],["Sector",sector],["ContractNumber",contractNumber], ["JobNumber",jobNumber],["DelegatorRole",role],["DelegatorTitle",delegatorTitle],["BusinessUnit",businessUnit]],
      completefunc: function(xData, Status) {
       //alert("completed title-" + strTitle);
      }
and when my variables are empty the list actually gets populated with the text string "Undefined"

TIA
Dean

Coordinator
Jun 24, 2011 at 3:21 PM
Edited Jun 24, 2011 at 4:07 PM

Dean:

Obviously, I can't control the changes which the jQuery team makes. I have to deal with those vagaries just like you.

My suggestion would be to build a small function which checks whether an attribute values is defined, and if it is return the value, else [your choice].

M.

Jun 24, 2011 at 4:05 PM

I have inserted the code below for my 'client' field as a test but it is not working, the alert for the Else statement shows that the client variable is 'undefined' i was expecting this to get matched in the If clause but it did not. any idea what I am doing wrong? I was thinking that if this worked then then i would create function that i could use for each field, am i going about this backwards?

TIA

Dean

$(document).ready(function() {
  $().SPServices({
     operation: "GetListItems",
     async: false,
     listName: "DelegateAssignmentInput",
     CAMLViewFields: "<ViewFields>"+
         "<FieldRef Name='Delegate' />"+
         "<FieldRef Name='DelegateAccount' />" +
         "<FieldRef Name='EndDate' />"+
         "<FieldRef Name='Delegator'/>"+
         "<FieldRef Name='DelegatorAccount'/>"+
         "<FieldRef Name='Client'/>"+
         "<FieldRef Name='Sector'/>"+
         "<FieldRef Name='ContractNumber'/>"+
         "<FieldRef Name='JobNumber'/>"+
         "<FieldRef Name='_Comments'/>"+
         "</ViewFields>",
     completefunc: function (xData, Status) {
      //alert(xData.responseXML.xml);
        $(xData.responseXML).find("[nodeName='z:row']").each(function() {
          delegate = $(this).attr("ows_Delegate").split(";#")[1];
          //alert ("Delegate "+ delegate);
          delegateAccount=$(this).attr("ows_DelegateAccount").split(";#")[0];
          //alert ("Delegate Account" + delegateAccount);
          delegator=$(this).attr("ows_Delegator").split(";#")[1];
          //alert ("Delegator "+ delegator);
          delegatorAccount=$(this).attr("ows_DelegatorAccount").split(";#")[0];
          //alert ("Delegator Account" + delegatorAccount);
          endDate = $(this).attr("ows_EndDate");
          //alert ("ows_DndDate "+ endDate);
          startDate = $(this).attr("ows_StartDate");
          client = $(this).attr("ows_Client");
          if (client=="undefined"){
           client=null;
           alert ("If Client-" + client);
           }
          else{
           client;
           alert ("else client-" + client);
          }
          alert ("Client 3 "+ client);
          sector = $(this).attr("ows_Sector");
          alert ("ows_Sector "+ sector);
          //alert ("Sector variable type " + typeof sector);
          contractNumber = $(this).attr("ows_ContractNumber");
          alert ("ows_ContractNumber "+ contractNumber);
          jobNumber = $(this).attr("ows_JobNumber");
          alert ("ows_JobNumber "+ jobNumber);
          comments = $(this).attr("ows__Comments");
          alert ("ows_Comments "+ comments);
    });
   }
  });
 });

Coordinator
Jun 24, 2011 at 4:15 PM

The value of client isn't the string "undefined", but the value undefined. Remove the quotes and you should be good.

M.

Jun 24, 2011 at 7:18 PM

I I have the following code, which works but my original approach was to put the jquery attr method in the parameter of the function call but that did not work. Am i doing something wrong, it seems like i should be able to do this type of call in one line

          jobNumber = $(this).attr("ows_JobNumber");
          jobNumber =checkForUndefined(jobNumber);

function checkForUndefined(x){
 if (x==undefined){
  return '';
 }else{
  return x;
 }
};

Dean

Coordinator
Jun 24, 2011 at 8:34 PM

Try this:

jobNumber = checkForUndefined($(this).attr("ows_JobNumber"));
function checkForUndefined(x){
  return x === undefined ? "" : x;
};

M.