Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
116
rated 0 times [  121] [ 5]  / answers: 1 / hits: 16338  / 11 Years ago, wed, april 3, 2013, 12:00:00

I wanted to wrap selected words in CKEditor in a <p> element.



From:



<p>This is a paragraph. And this is Selected text.</p>


To:



<p>This is a paragraph. And this is</p>
<p class=myclass>Selected text.</p>


I found some code:



( function() {
CKEDITOR.plugins.add( 'qna', {
   init: function( editor ) {
     editor.addCommand( 'insertQnA', {
       exec : function( editor ) {   
         if(CKEDITOR.env.ie) {
           editor.getSelection().unlock(true);
           var selected_text = editor.getSelection().getNative().createRange().text;
         } else {
           var selected_text = editor.getSelection().getNative();
         }
         editor.insertHtml('[before]' + selected_text + '[after]');
       }
     });
     editor.ui.addButton( 'qna', {
label: 'Insert QnA',
command: 'insertQnA',
icon: this.path + 'images/qna.png'
});
   }
});
})();


I wanted to replace the [before] and [after] with <p classmyclass> and </p> but it doesn't work.



I'm quite a newbie in JS/Jquery. I hope you can shed some light on it for me.



EDIT: From Spon's reply.



( function() {
CKEDITOR.plugins.add( 'qna', {
  init: function( editor ) {
    editor.addCommand( 'insertQnA', {
      exec : function( editor ) {   
editor.applyStyle(new CKEDITOR.style({
Element : 'p',
Attributes : { class : 'Myclass' },
Styles : { color : '#ff0000','font-family' : 'Courier'}
}));
      }
    });
    editor.ui.addButton( 'qna', {
label: 'Insert QnA',
command: 'insertQnA',
icon: this.path + 'images/question.png'
});
  }
});
})();


The above code wraps the selected text/words in a <span> element for some unknown reason.



Example:



From...



<p>This is a paragraph. And this is Selected text.</p>


To...



<p>This is a paragraph. And this is <span>Selected text.</span></p>


This is not what I want.


More From » ckeditor

 Answers
8
exec : function( editor ) {
var selected_text = editor.getSelection().getSelectedText(); // Get Text
var newElement = new CKEDITOR.dom.element(p); // Make Paragraff
newElement.setAttributes({style: 'myclass'}) // Set Attributes
newElement.setText(selected_text); // Set text to element
editor.insertElement(newElement); // Add Element
}


This will fix it.. This is the Exec part as you can see.


[#79149] Tuesday, April 2, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
terrence

Total Points: 120
Total Questions: 115
Total Answers: 87

Location: England
Member since Fri, May 22, 2020
4 Years ago
terrence questions
Sat, Jun 5, 21, 00:00, 3 Years ago
Wed, Jun 17, 20, 00:00, 4 Years ago
;