Empty P tags in SharePoint

So, a common complaint I’ve seen from clients is how SharePoint 2010 inserts unnecessary paragraph tags within rich text content. Now, until Microsoft changes how this functionality works I’d like to present a simple jQuery solution for semi-intelligently hiding these unneeded P tags. What this does is run through the P tags on the current page. If there are no child tags and there is at most one character in it (as SharePoint does that escaped out placeholder character), then it will set display:none.

$(document).ready(function () {
$(“p”).each(function (index) {
var len = $(this).children().length; //get number of children
var text = $(this).text().length; //get length of inner text
if (len == 0 && text < 2) { //if there’s no children and only 1 or 0 characters hide the P tag
$(this).css(“display”, “none”);
  1. #1 by Tom on July 21, 2011 - 8:21 am

    Sharing our pain with the world – nice! Let it be heard from shore to shore… and up to Redmond!!!

    • #2 by Maarten on July 21, 2011 - 9:22 am

      This will go in tonight btw Tom 😉

  2. #3 by Richard on March 23, 2012 - 4:00 am

    Will this not cause a performance hit on each page it is applied to – especially if there are dozens of p tags to iterate through?

    • #4 by Maarten on March 23, 2012 - 2:15 pm

      It does in our testing though it’s at most 100ms even on less capable browsers like IE7. Currently short of an event handler there is no other method to really address this issue. I am meeting with the product team next week to go through possible resolutions via a CU though 🙂

(will not be published)

%d bloggers like this: