1

I have this code that uses SPServices to get the value from a List column...in this case the Body column from the Announcements List. The code works and it will append the Body to the HTML list.

$(document).ready(function() {
  $().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "Announcements",
    CAMLViewFields: "<ViewFields><FieldRef Name='Body' /></ViewFields>",
    completefunc: function (xData, Status) {
      $(xData.responseXML).SPFilterNode("z:row").each(function() {
        var liHtml = "<li>" + $(this).attr("ows_Body") + "</li>";    
        alert($(this).attr("ows_Body")); 
        $("#tasksUL").append(liHtml); 
      });
    }
  });
});

However, I have a couple of challenges that I can't seem to figure out.

I'm trying to get the value from the Body column in the SharePoint List, for this purpose, we can say it has a value of "Hello World" however:

$(this).attr("ows_Body")

returns

"<div class="ExternalClassD831897642D94F6B816CFF029C96787F"><p>Hello World<br></p></div>"

instead of just returning "Hello World"

How can I change the code to get this output without the 'div' and 'p' tags ?

The reason for this is because I want to be able to compare this value to another and execute some code whether it is true of false

1

I think you have created the body field as rich enhanced type.You can create div and append the field value then get the innerText as follow:

$(document).ready(function() {
   $().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "Announcements",
    CAMLViewFields: "<ViewFields><FieldRef Name='Body' /></ViewFields>",
    completefunc: function (xData, Status) {
    $(xData.responseXML).SPFilterNode("z:row").each(function() {
    var liHtml = "<li>" + $(this).attr("ows_Body") + "</li>";  
    var div= document.createElement('DIV');
    div.innerHTML = $(this).attr("ows_Body");  
    alert(div.innerText); 
    $("#tasksUL").append(liHtml); 
    });
   }
  });
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.