0
votes

OK, I have a JavaScript created section on a page and I want to return a summary only not the entire text. i.e. first 50 whole words and as ive mentioned before, JavaScript isnt my thing but it has to be JS not Jquery.

I have written the script but it returns unexpected results so would be grateful if someone could take a look and point out where Im going wrong.

My code:

var ap = document.createElement("P");
        var stext = value[i].Summary.split(" ", 50)
        ap.appendChild(document.createTextNode(stext));
        ar.appendChild(ap);

Which returns:

Lorem,ipsum,dolor,sit,amet,,consectetur,adipisicing,elit,,sed,do,eiusmod ,tempor,incididunt,ut,labore,et,dolore,magna,aliqua.,Ut,enim,ad,minim, veniam,,quis,nostrud,exercitation,ullamco,laboris,nisi,ut,aliquip,ex,ea, commodo,consequat.,Duis,aute,irure,dolor,in,reprehenderit,in,voluptate, velit,esse,cillum,dolore,eu,fugiat

What I want to return is:

Lorem ipsum dolor sit amet consectetur adipisicing elit sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat

1
value[i].Summary.split(" ", 50).replace(',',' ')Bharadwaj
looks like you just need to join your split.. try .join(' ') after your split. (also dont forget the closing ; on that linehaxxxton
Thanks @haxxxton yours was the solution.CloudSurferUK

1 Answers

4
votes

Use Javascript array Join. Simply like that:


var ap = document.createElement("P");
var stext = value[i].Summary.split(" ", 50);
stext.join(" ");

Here is the link to the MDN for join()