You need to be logged in to post in the forum - Log In
An active JCE Pro Subscription is required to post in the forum - Buy a Subscription
- Support
- Forum
- JCE Editor
- removing empty p tags at the end of an article on save
Support is currently Online
Official support hours:
Monday To Friday
From 09:00 To 17:00 Europe/London (BST)
From 09:00 To 17:00 Europe/London (BST)
Hi! We often notice thst content manager leave empty p tags at the end of the article content. Is there a way to remove thoe empty p tags when the article is saved? So only the empty p tages at the end. Not when they are somewhere in between.
We could do this with some frontend scripting but I think I would prefer to remove them before saving them to the database.
We could do this with some frontend scripting but I think I would prefer to remove them before saving them to the database.
jjonker
changed the title from removing empty p tags aat the end of an article to removing empty p tags at the end of an article on save — 1 year ago
Create a file called editor.js in media/jce/js/ and add the following to the file:
tinyMCE.onAddEditor.add(function (mgr, ed) {
ed.onGetContent.add(function (ed, o) {
o.content = o.content.replace(/(<p[^>]*>( | |\s|\u00a0|)<\/p>)$/, '');
});
});
Just because you're not paranoid doesn't mean everybody isn't out to get you.
Hii Ryan! I tried that but nothing seems to happen.
There was no folder called js (in media/jce) so I crerated one. Added the editor.js file with the code to it.
We are on Joomla 4,1.5 / PHP 8.0.21
JCE is up to date.
What is the intended function of this script? To remove all empty p tags on save so they are removed before the content is placed in the database? When saving it in the backend? Or is it a script to remove the p tags on the frontend?
When a new p tag is created in the editor it does contain a non-breaking space. Would that influence this script?
Would this only delete the last p tag found or also maybe 3 or 4 empty p tags at the end? Like this:
<p> </p>
<p> </p>
<p> </p>
There was no folder called js (in media/jce) so I crerated one. Added the editor.js file with the code to it.
We are on Joomla 4,1.5 / PHP 8.0.21
JCE is up to date.
What is the intended function of this script? To remove all empty p tags on save so they are removed before the content is placed in the database? When saving it in the backend? Or is it a script to remove the p tags on the frontend?
When a new p tag is created in the editor it does contain a non-breaking space. Would that influence this script?
Would this only delete the last p tag found or also maybe 3 or 4 empty p tags at the end? Like this:
<p> </p>
<p> </p>
<p> </p>
The forum extension has removed the non-breaking space from the script I posted above. Please use the editor.js file in the attached zip.
It removes the last empty paragraph in content when the content is extracted from the editor, ie: when saving, or switching to the Code tab.
Only the last empty paragraph is removed.
What is the intended function of this script? To remove all empty p tags on save so they are removed before the content is placed in the database? When saving it in the backend? Or is it a script to remove the p tags on the frontend?
It removes the last empty paragraph in content when the content is extracted from the editor, ie: when saving, or switching to the Code tab.
Would this only delete the last p tag found or also maybe 3 or 4 empty p tags at the end? Like this:
<p> </p>
<p> </p>
<p> </p>
Only the last empty paragraph is removed.
Just because you're not paranoid doesn't mean everybody isn't out to get you.
Hi Ryan, thanks. It's working now. Great.
Is there a way to alter the script so it will remove all empty tags at the end of the article? So even when there are two or three or even more?
It can be done on the frontend with this jQuery script:
It uses 'each' and then 'reverses' the found p tags to remove them at the end and work it's way back untill the first non-empty p is reached. Not sure how to 'translate;' this to the function.js file...
Is there a way to alter the script so it will remove all empty tags at the end of the article? So even when there are two or three or even more?
It can be done on the frontend with this jQuery script:
jQuery(".com-content-article__body").each(function() {
jQuery(jQuery("p", this).get().reverse()).each(function() {
if (jQuery.trim(jQuery(this).text()).length === 0) {
jQuery(this).remove();
} else {
return false;
}
});
});
It uses 'each' and then 'reverses' the found p tags to remove them at the end and work it's way back untill the first non-empty p is reached. Not sure how to 'translate;' this to the function.js file...
Hi Ryan!
1) Removing multiple empty p tags at the end of the content works great now.
2) When I put place multiple empty p tags somewhere in the middle of the content (like 3 of 4 empty p tags between p tags WITH cntent) this script reduces those multiple p tags to just one empty p tag, Is that what you intended? It think just deleting empty p tags ONLY at the end of the content will be fine. Because those are not very visible tot the user. When a user intentionally add some empty line in between (non-empty) paragraphs they should remain there?
1) Removing multiple empty p tags at the end of the content works great now.
2) When I put place multiple empty p tags somewhere in the middle of the content (like 3 of 4 empty p tags between p tags WITH cntent) this script reduces those multiple p tags to just one empty p tag, Is that what you intended? It think just deleting empty p tags ONLY at the end of the content will be fine. Because those are not very visible tot the user. When a user intentionally add some empty line in between (non-empty) paragraphs they should remain there?
I thin may using 'break' in stead of 'continue' (line 24) might do the trick?
Yes, that's it!
Just because you're not paranoid doesn't mean everybody isn't out to get you.
Thanks Ryan. We can use this on our sites like this.
Would you consider adding an option in the JCE settinfs for this? So we (and other users) don't need to add the file maunally but can just select if they want this option turned on or not? Maybe in the JCE profile?
But again, this works for us ;-) So thanks for taking the time to help out with this!
Would you consider adding an option in the JCE settinfs for this? So we (and other users) don't need to add the file maunally but can just select if they want this option turned on or not? Maybe in the JCE profile?
But again, this works for us ;-) So thanks for taking the time to help out with this!
- Page :
- 1
There are no replies made for this post yet.
Be one of the first to reply to this post!
Be one of the first to reply to this post!