I am trying to remove a class injected my sharepoint, since it's breaking my modal dialog box. This is the code:

function popup(myTitle, myBody) {

//the modal dialog
    var element = document.createElement('div');
        html: element,
        title: myTitle

function itemTemplate(ctx) {
    if (ctx.ListTitle != window.News.LIST_NAME) { return RenderItemTemplate(ctx); }
    //set all variables

    var newBody = ctx.CurrentItem["Body"];
    var bodyTxt = "<div></div>";

    //set title styling
    var titles = "<div><a class= 'newsApp-title' href='#' onclick=\"popup('{{Title}}','{{Body}}')\">{{Title}}</a></div>";

    var expires = "<div class='newsApp-date'>{{Expires}}</div>";

    //replace the popup section with the title and body of the current item
    titles = titles.replace(/{{Title}}/g, ctx.CurrentItem["Title"]);
    titles = titles.replace(/{{Body}}/g, bodyTxt);

    //replace the expires section with the current item expiration
    expires = expires.replace(/{{Expires}}/g, ctx.CurrentItem["Expires"]);

    var myContent = titles + expires;
    return myContent;  

if I just make the html: part of the modal equal ctx.CurrentItem['Body'], it breaks because SharePoint returns that item in this format:

<div class="ExternalClassF13D31784EFA43BA89316110B33AB038"><p>text<p></div>

What I'm trying to do, is just pull the text from that element to get around this. Right now, I'm getting the error "TypeError: Object doesn't support property or method 'getElementsByTagName' "

I can't think of a better way to do it, since everything else I've tried hasn't worked. Any help would be greatly appreciated.


1 Answer 1


The problem is that AFAIK when you are accessing ctx.CurrentItem["Body"] SharePoint is returning a string. It is not like it is returning an actual DOM element you can call getElementsByTagName on.

That said, I also think it is not worthy to try to manually "parse" the returned string, or convert it to a DOM element to be able to navigate it until you can extract the actual text. Both solution would probably end up begin too complex, prone to error and strongly coupled to the specific structure of the html fragment you expect (for example, in your question your code would be working on the assumption that the body contains one <p> tag).

I have checked some documentation, and I have remembered a method called "FieldValuesAsText" available on the SPListItem class. It also has a JSOM equivalent.

This question "get_fieldValuesAsText usage example" provides some simple examples on how to use the method.

You should be able to adapt the samples to your situation. Just notice that the sample assumes that you have to manually load the context - you don't really specify where your code is running, so it is possible that you may have to alter the code a little if you are inside an edit form or such. If need arises, try to provide some more details and we may be able to help.

  • I tried to use newBody.getElementsByTagName("p")[0].innerHTML, but I got the error message: TypeError: Object doesn't support property or method 'getElementsByTagName' Feb 23, 2016 at 20:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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