Home › Forums › General discussion › Remove first p tag from rich text fields
Tagged: RTF
- This topic has 8 replies, 2 voices, and was last updated 7 years, 10 months ago by Roelof Meijer.
-
AuthorPosts
-
-
January 13, 2017 at 16:49 #15034
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.
-
January 16, 2017 at 20:14 #15072
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.
-
January 19, 2017 at 14:14 #15155
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
-
January 23, 2017 at 20:26 #15220
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
-
January 24, 2017 at 14:37 #15239
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? -
January 24, 2017 at 21:45 #15250
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
-
January 25, 2017 at 08:16 #15260
Hi Alexander,
Thanks for your reply.
Unfortunately this does not make a difference. -
January 28, 2017 at 11:30 #15313
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
-
February 3, 2017 at 09:03 #15454
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.
-
-
AuthorPosts
- You must be logged in to reply to this topic.