Remove first p tag from rich text fields

Home Forums General discussion Remove first p tag from rich text fields

Tagged: 

Viewing 7 reply threads
  • Author
    Posts
    • #15034
      Roelof Meijer
      Participant

        I want to remove the first p tag from an enhanced richt text field. This is the paragraph that SharePoint wraps the text in by default. And this p tag is prepended by a div with a classname that starts with “ExternalClass”
        I created this function in the Custom JS tab:

        
        
        function hanldeRTEfields (){
                var pTags = $("div[class^='ExternalClass']>p:first-child");
                pTags.contents().unwrap();
              }

        This function is called in the rule that triggers when form is saved. The result is that the rich text fields are empty. How come? Is there another way to change the content of a rich text field on save?o

        • This topic was modified 7 years, 11 months ago by Roelof Meijer.
      • #15072
        Alexander Bautz
        Keymaster

          Hi,
          You can try this one that replaces all p-tags with span-tags, but I’m not sure how it will work out:

          function dffs_PreSaveAction(){
              var rteContents = getFieldValue("FIELDINTERNALNAME").split("<p>").join("<span>").split("</p>").join("</span>");
              setFieldValue("FIELDINTERNALNAME",rteContents);
          }

          Add the code to the Custom JS in DFFS, and replace the “FIELDINTERNALNAME” with your fields name.

          Alexander

          • This reply was modified 7 years, 11 months ago by Alexander Bautz. Reason: Fixed an error in the code snippet
          • This reply was modified 7 years, 11 months ago by Alexander Bautz.
          • #15155
            Roelof Meijer
            Participant

              Hi Alexander,

              Thanks for your reply. I am now half way there. When I combine your dffs_PerSaveAction with my code it works for editting an item. But not when inserting a new item. Then I’m still left with the div and P tag

              So this script helps when editing an item

              
              
              function dffs_PreSaveAction(){
                 //var rteContents = getFieldValue("Nature").split(/<p>|<\/p>/).join("<span>");
                 //setFieldValue("Nature",rteContents);
                  var pTags = $("div[class^='ExternalClass']>p:first-child");
                  pTags.contents().unwrap();
              }

              The split/join statement (that you suggested) did not help. The html in the text field still kept the div and p tag (both on insert and edit).

              Is there maybe an equivalent function in dffs that fires like an “item-added” trigger

          • #15220
            Alexander Bautz
            Keymaster

              Hi,
              I don’t think the div with class “ExternalClass” is actually added until it is saved. This mean you must use another “selector” to get the value in NewForm. Are you sure it is the div that adds your whitespace? – I actually think it is the p-tag.

              I’m not sure it does any difference, but I had an error in my original code snippet that I edited after I first posted it – look at it above and see if it might help.

              Alexander

            • #15239
              Roelof Meijer
              Participant

                Hi,
                This one works, but does too much. All p tags are replaced. And what is needed is that only the first p tag is replace and only then, when there is no preceding text. Otherwise we would and up replacing every p tag on subsequent updates. So I would need a split/join of the first occurrence only, which is not preceded by text. Is that possible?

              • #15250
                Alexander Bautz
                Keymaster

                  I’m not sure how this works when you use it in Word, but try setting margin=0 in the first P in NewForm using this code:

                  function dffs_PreSaveAction(){
                      var rteContents = getFieldValue("FIELDINTERNALNAME").replace("<p>","<p style='margin:0;'>");
                      setFieldValue("FIELDINTERNALNAME",rteContents);
                  }

                  Alexander

                • #15260
                  Roelof Meijer
                  Participant

                    Hi Alexander,
                    Thanks for your reply.
                    Unfortunately this does not make a difference.

                  • #15313
                    Alexander Bautz
                    Keymaster

                      Maybe you could rearrange your word file setup so the padding above the RTE doesn’t matter?

                      If you maybe use a setup like this:

                      FieldLabel1 | FieldValue1 = two columns
                      —————————————-
                      FieldLabel2 | FieldValue2 = two columns
                      —————————————-
                      FieldLabel for RTE = one column (colspan = 2)
                      —————————————-
                      FieldValue for RTE = one column (colspan = 2)
                      —————————————-

                      Alexander

                    • #15454
                      Roelof Meijer
                      Participant

                        I managed to convince the end users that it would be better to go for plain text fields (Multi line). So this is not a problem to me anymore. It still is intriguing riddle, though.
                        Thanks for the help.

                    Viewing 7 reply threads
                    • You must be logged in to reply to this topic.