Using Javascript to apply unique styling to CKEditor text

Currently, I am working on a self-contained web page and utilizing CKEditor 4.6.2 for input purposes. The issue I am facing at the moment is attempting to apply a CSS class style to text that has been inserted via JavaScript. My CSS is located in the head tag without any special configurations. As per the CKEditor documentation, I have defined a styleset.

CKEDITOR.stylesSet.add('custom_css_styles', [
      // Block-level Styles
      { name: 'My Custom Block', element: 'p',
        styles:{
          'color': 'dimgray',
          'padding-left':'15px',
          'border-left-style':'solid',
          'border-width':'3px',
          'border-color':'#52bab3',
          'margin-left':'2px',
        }
      },
      { name: 'CustomInline', element: 'span', attributes: {'class': 'redText'}
      }
    ]);

This is then added in CKEditor settings as shown below:

CKEDITOR.replace('LogBook', {
      language: 'en',
      uiColor: '#9AB8F3',
      toolbar: [
        ['Undo','Redo','-'],
        ['Bold','Italic','Underline','Strike','-'],
        ['RemoveFormat','-'],
        ['NumberedList','BulletedList','-'],
        ['HorizontalRule'],
        '/',
        ['Styles','-','Source','-','About','Maximize'],
      ],
      removeButtons: 'Subscript,Superscript,Cut,Copy,Paste,Anchor,Scayt,PasteText,PasteFromWord,Image,SpecialChar,Link,Unlink,Table,Indent,Outdent,Blockquote,Format',
      removePlugins: 'wsc,scayt',
 stylesSet:'custom_css_styles',
 allowedContent: true
    })

Despite turning off content filtering and having a custom function that inserts code with classes, I am still struggling to style paragraph elements within the editor using Javascript, as demonstrated below:

<p class="My Custom Block">Some text here</p>

I also attempted directly applying styles from my stylesheet like so:

<p class="redText">Some text here</p>

However, the paragraph elements remain unstyled. Any assistance offered would be greatly appreciated. Thank you.

Answer №1

After searching extensively, I finally stumbled upon a valuable solution (shared by surtyaar at the end) for adding a class to CKEditor elements.

CKEDITOR.addCss( '.redText { border-bottom: 1px dotted red }' );

Remember to execute this before declaring any CKEditor instances.

Referencing the documentation on CKEditor's official website.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Verifying the angular form without the need for a submit button

I'm currently utilizing angular.js form validation and I'm looking to implement validation without the use of a form submit button. Below is my HTML code: <form name="userForm"> <input type="hidden" ng-model="StandardID" /> < ...

Display HTML content using JavaScript only when a checkbox is selected

Currently, I am updating an HTML form to show additional subsets of questions based on the selection of a "parent" checkbox. The current implementation works well, but I am wondering if there is a more efficient way to achieve this without having to rewrit ...

Tips for preventing a React component from re-fetching data when navigating back using the browser button

In my React app using Next.js and the Next Link for routing, I have two pages set up: /product-list?year=2020 and /product-list/details?year=2020&month=4 Within the pages/product-list.js file, I am utilizing React router to grab the query parameter ye ...

Display issues occur with Bootstrap 4.2.1 flex-box layout columns exceeding the browser edge

Why does COLUMN02 extend beyond the browser's edge when using flex-basis for column widths? What mistake am I making? Unfortunately, it doesn't display correctly in the code snippets here but works in the fiddle. TIA! http://jsfiddle.net/dragont ...

Changing text content of an element with dynamic formatting

When a link is clicked on, I want to dynamically set the text. Here is the current code: <a href="www.somelinkhere.com" onclick="getElementById('setText').innerHTML='text to replace'" target="someIFrame" ...

Align a div to the center horizontally at the top of the screen

Is there a way to vertically center text within a div and also horizontally center the div at the top of the screen? Visit this link for more details <div id='custom'> example text </div> #custom{ width: 50%; background: #FF00 ...

Can you elaborate on how a numeric value is passed as an argument to the function within a React Tic Tac Toe tutorial when handling a click event?

I'm a bit confused about how the onClick event works with the handleClick function and passing numbers as arguments. Is this related to closures? Can someone explain how the numbers are passed as arguments when the click event happens? Also, should ...

Tips for navigating through a complex object returned from an API.integration**How to

Looking at this API response, I need to extract all the appId and userInfo values. How can I efficiently iterate through this response? { "status_code": "SUCCESS", "status": "SUCCESS", "message" ...

Fade in elements as you scroll using Jquery

I'm currently working on a timeline feature for my website, and I want each element with the class '.event' to fade in as the user scrolls through the timeline. However, I'm running into an issue where all elements fade in simultaneousl ...

Countdown malfunction: wrong date displayed

Utilizing the Countdownjs library in my project is resulting in an incorrect day count. Incorporating AngularJS, here is the custom directive I've implemented for the countdown: .directive('tempoPercorrido', function($interval){ ret ...

What is the reason for encodeURIComponent not encoding single quotes or apostrophes?

Although the escape() function was deprecated and replaced by encodeURIComponent, there is an issue with encodeURIComponent as it doesn't encode the single quote/apostrophe character. This poses a problem when trying to escape apostrophes in someone&a ...

What is the best way to ensure that a React app is always displayed in full screen

I am facing an issue while developing an app with React and Material UI. I am trying to display the app in full-page mode, but unable to achieve it successfully. Currently, it appears like this: https://i.sstatic.net/Hg0CA.png Here is my code snippet from ...

I'm stuck on finding the solution for PROJECT EULER P4

Challenge 4 Palindrome numbers read the same forwards and backwards. The largest palindrome that can be formed by multiplying two 3-digit numbers is 9009 = 91 × 99. Your task is to find the largest palindrome made from the product of two 3-digit numbers. ...

Extract information from various files stored in Firestore

I have been struggling to retrieve data from multiple documents despite numerous attempts. The screenshot below displays that I have a collection of 3 documents, and my inquiry is how to extract data from each of them. https://i.stack.imgur.com/bFrIG.png ...

Tips for showcasing unique keywords in Ace Editor within the Angular framework

Can anyone help me with highlighting specific keywords in Angular using ace-builds? I've tried but can't seem to get it right. Here's the code snippet from my component: Check out the code on Stackblitz import { AfterViewInit, Component, ...

Tips for choosing or unselecting a row within a Table utilizing the Data Grid Mui

Is it possible to implement row selection and deselection in Mui without using the checkboxSelection prop? I am looking for a way to achieve this functionality in @mui/x-data-grid when clicking on a row. Below is the code snippet from the Table Component ...

Tips for utilizing Mongoose populate with a subdocument within the identical model?

This is the model for my latest project. const customHeaderSchema = new Schema({ header: { type: String, required: true, }, }); const customFeatureSchema = new Schema({ title: { type: String, required: true, ...

Switching Logo Image when Menu Button is Clicked

I'm looking to adjust the color of my logo when the menu button is clicked to match the overlay style. Currently, I managed to switch from a black logo to a white logo. However, when attempting to close the menu, it doesn't revert back to the bl ...

Employing condition-based require() statements in JavaScript

I am currently experimenting with conditionally loading modules that are Vue components. Review the code snippet below for a better understanding: index.js My goal is to dynamically load different components based on the window URL (since I cannot use vu ...

Error encountered while invoking web server method in C# through ajax resulting in a 500 Internal Server Error

Occasionally encountering a 500 internal server error when calling a server method from an AJAX request has left me perplexed. The inconsistency of the issue, sometimes working fine and sometimes not, is baffling. To add to the confusion, no changes were m ...