• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 735
  • Last Modified:

JQuery Sharepoint List Issue with If Function


I have a java script which I am using to try and return the number of values in a sharepoint list that meet the criteria of being created in Period 2.

The script also does a count of the total number of entries in the list.

Currently, the script returns a result only if the following section is commented out. I've debugged the script and can see that there is a problem arround the datavalues that it is populated with. Screenshot attached.

//count number of lines in period
//      if ( periodValue === $node.attr("EM_REPORTING_PERIOD") ) {
//	numOpenedInPeriod++;}

Open in new window

Can anybody tell me what is wrong with the script and why it doesn't return a defined value for the sharepoint column to compare against?

I added  "alert($node.attr("EM_REPORTING_PERIOD"));" to see what was in the attribute & it just reports undefined.


Full Code:

<script type="text/javascript" src="http://mynationwideteam/sites/fin_comms_tech_serv/Javascript/jquery-1.8.1.min.js"></script>
<script type="text/javascript" src="http://mynationwideteam/sites/fin_comms_tech_serv/Javascript/jquery.SPServices-0.7.1a.min.js"></script>
<script language="javascript" type="text/javascript">

$(document).ready(function() {
   //define local variables

   var numOfLines = 0, numOpenedInPeriod = 0 ;
   var periodValue = 2; // this will be set by drop down on going

    operation: "GetListItems",
    async: false,
    listName: "EM Exception Log",
    CAMLViewFields: "<ViewFields><FieldRef Name='Title' /></ViewFields>",
CAMLQuery: "<Query><Where><Neq><FieldRef Name='ID' /><Value Type='Counter'>0</Value></Neq></Where></Query>",

    completefunc: function (xData, Status) {
      $(xData.responseXML).SPFilterNode("z:row").each(function() {
        var $node = $(this);

//count number of lines in period
     if ( periodValue === $node.attr("EM_REPORTING_PERIOD") ) {	numOpenedInPeriod++;}





var output = "<table><tr><td><h3>Total Number of Lines: " + numOfLines + "</h3></td></tr>" + "<tr><td><h3>Total Open In Period 2: " + numOpenedInPeriod + "</h3></td></tr></table>";

//Append output to the DOM.
$("#results").append( output );

<div id='results'></div>

Open in new window

  • 2
  • 2
1 Solution
you could try logging to console $node. Perhaps it's not what you think it is.


If you view this in firebug, you'll see all the info on the object and should be able to work out if you have the correct element.
bedsingarAuthor Commented:

Firebug is a Firefox addin isn't it? - I'm unfortunately restricted to using Internet explorer and can't install addins due to the admin policy.

Can I do this in IE Debug also? (As per the screenshot)

Well, if $node.attr("EM_REPORTING_PERIOD") returns undefined is because that element isn't on your page.

It could be two things:
1) $node is not defined, maybe because the node you are trying to load and reference on your javascript is not on the page.

2) There is no EM_REPORTING_PERIOD attribute on the element referenced by $node.

You can check on your alert which of two options is just doing alert($node); if the element is on the page and is well referenced you should receive something like "[object Object]", if you receive here undefined then check what's happening with this node and why it's not being correctly loaded.

If the node is correctly loaded you should check on the page why it doesn't has the attribute you are asking for "EM_REPORTING_PERIOD" maybe there is a spelling error when it's being created on the page code, or maybe it's not being loaded...
Unfortunately the Dev Tools in IE are somewhat lacking. In firebug if you console.log an object you're able to click the item in the log and view all it's details. In IE when you do the same you literally just get a line in the log saying:

LOG: [object Object]

It will at least tell you if you've got an object in the variable but you aren't getting much more info than that.
bedsingarAuthor Commented:
Found it finally ... $node.attr("ows_EM_REPORTING_PERIOD")

I missed off the ows_ .... In the mean time I'd completely re-written the code to take another approach.

Thanks for all your contributions.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now