I want to replace all {baseurl} keywords to proper url in TinyMCE editor. How can i do that?
For example if user will add HTML in editor <img src="{baseurl}/image.jpg" /> i want to see this image in TinyMCE editor - so this will be replaced to <img src="http://mydomain.com
/image.jpg" />
Any ideas?
Here is the code that will replace your editor content. But you will need to do this action at the correct time.
var editor = tinymce.get('my_editor_id'); // use your own editor id here - equals the id of your textarea
var content = editor.getContent();
content = content.replace(/{\$baseurl}/g, 'http://mydomain.com');
editor.setContent(content);
With this solution I was able to modify the content on-the-fly, without replacing the content as whole:
tinymce.init({
setup: (editor)=>{
editor.on('init', ()=>{
$(editor.contentDocument).find('a').prop('title', 'my new title');
});
}
});
Maybe it helps someone :)
I used a very simple code working good with me
tinymce.get("page-content").setContent(''); // 'page-content' as the textarea id
tinymce.get("page-content").execCommand('mceInsertContent', !1, 'New content data');
Related
I try to use the same ckeditor replace several time in a ajax form:
... <textarea id="textareaId"></textarea> ...
And a javascript code
CKEDITOR.replace('textareaId');
$('#textareaId').val("first");
CKEDITOR.replace('textareaId');
$('#textareaId').val("second");
Can somepody know why id does not show "second" instead of "first" in the textarea with ckeditor? And how to get the correct value $('#textareaId').val()?
Thank you
I think you should force destroying instance before replace again.
var editor = CKEDITOR.instances['textareaId'];
if (editor) {
editor.destroy(true);
}
CKEDITOR.replace('textareaId');
Ids should be unique, so (I don't use CKEditor btw so no clue what this is trying to achieve but am explaining how ids should be used):
<textarea id="textareaId"></textarea>
<textarea id="textareaId2"></textarea> <!-- changed id -->
CKEDITOR.replace('textareaId');
$('#textareaId').val("first");
CKEDITOR.replace('textareaId2'); /* new id for text area */
$('#textareaId2').val("second");
Here's my html:
...<div class="myclass"><label>Default</label></div>
my javascript:
ace.edit(el); // where el is the dom element div.myclass shown above
The editor is rendered properly!
The editor only comes up with "Default". No html to edit.
When I look at the code in debug mode on chrome, I see that the edit function retrieve the value of my element using (line 18474):
value = dom.getInnerText(el);
Which in turn result in:
return el.textContent;
then el innerHTML is blanked using (18475):
el.innerHTML = '';
This seems very strange to me. el.innerHTML correctly displays
"<label>Default</label>"
Is it me or there's something wrong?
How can I edit the html content of a div using Ace editor if this is normal behavior? What am I missing?
Thanks
Found an answer here
https://github.com/ajaxorg/ace/issues/519
I'll try it and let you guys know if this doesn't solve it
You basically need to
el = document.getElementById("editor"); text = el.innerHTML editor = ace.edit(el) editor.session.setValue(text)
Ace uses textContent instead of innerHTML because it's not possible to pass arbitrary text using innerHTML. E.g if you have '<div class="myclass">' innerHTML will make it '<div class="myclass"></div>' and <script>alert('haha')</script> will display alert dialog.
You can use textarea or xmp tag, to not make browser parse the text but there again you need to escape </textarea. So the sanest way is to escape special html characters like < and & and use textContent.
If your code content has HTML use textarea as the container to call on Ace Editor
Atention
On C# and Javascript use innerText a not ~~innerHtml~~.
Example
HTML
<textarea id="aceeditorcontent" runat="server" clientidmode="Static"></textarea>
<script>
var aceeditor = ace.edit("aceeditorcontent", {
theme: "ace/theme/chrome",
mode: "ace/mode/html_ruby",
maxLines: Infinity,
showPrintMargin: false,
wrap: true,
autoScrollEditorIntoView: true,
enableSnippets:true
});
</script>
Csharp
aceeditorcontent.InnerText = "<html>......</html>";
javascript
document.getElementById("aceeditorcontent").innerText = "<html>......</html>";
I'm a beginner on Javascript/TinyMCE and I try to understand how is it possible to get the HTML content from the editor, and show it with a simple alert() function.
I've this minimalist config on my HTML page :
<div id="tiny">
<script type="text/javascript">
tinyMCE.init({
// General options
mode : "specific_textareas",
editor_selector : "mceEditor"
});
</script>
</div>
<form method="post" action="somepage">
<textarea id="myarea1" class="mceEditor">This will be an editor.</textarea>
</form>
On the TinyMCE Website, They explained that i have to use this :
// Get the HTML contents of the currently active editor
console.debug(tinyMCE.activeEditor.getContent());
And here too
tinymce.activeEditor.getContent()
I don't know why it doesn't work
Someone have an idea ?
I don't know why it doesn't work
It's not working because
console.debug(tinyMCE.activeEditor.getContent());
tinymce.activeEditor.getContent();
these statements are not being executed.
Try to follow this FIDDLE ....
tinyMCE.init({
// General options
mode : "specific_textareas",
editor_selector : "mceEditor"
});
Function for getting content ....
function get_editor_content() {
// Get the HTML contents of the currently active editor
console.debug(tinyMCE.activeEditor.getContent());
//method1 getting the content of the active editor
alert(tinyMCE.activeEditor.getContent());
//method2 getting the content by id of a particular textarea
alert(tinyMCE.get('myarea1').getContent());
}
Get the content of editor on button click ...
<button onclick="get_editor_content()">Get content</button>
Maybe it's the case? Your variable is tinyMCE, but you are calling getContent() on tinymce. JS is case sensitive ;)
I was looking for a solution and tried some of the above then went looking more on the tinymce documentation and found this to be effective.
Using the tiny mce 4
function getHTML()
{
tinymce.activeEditor.on('GetContent', function(e) {
console.log(e.content);
});
}
just call that function with an onclick and see what the results are...
My source is:
http://www.tinymce.com/wiki.php/api4:class.tinymce.ContentEvent
TinyMCE creates an iframe under the format '#textareaid'+'_ifr' So by using jquery we can query the HTML contents of the text area you like
the iframe id will be your textarea Id with "_ifr" appended to that. So you can extract HTML contents from tinyMce
$('#textareaId_ifr').contents().find("html").html();
I have a text <info>SOME CONTENTS GOES HERE</info>
How i can remove this text from the editor when I click on a button (custom button) using javascript function. I used this code:
dom.remove(dom.getParent(selection.getNode(), 'info'));
But it is showing an error. Is there any solution?
Thanks in advance.
tinyMCE offers a method under DOMUtils which is tinymce.dom.DOMUtils/remove
// Removes all paragraphs in the active editor
tinyMCE.activeEditor.dom.remove(tinyMCE.activeEditor.dom.select('p'));
// Removes a element by id in the document
tinyMCE.DOM.remove('mydiv');
So in your case since you want to remove <info> and what's inside then you should write something like :
// Removes all paragraphs in the active editor
tinyMCE.activeEditor.dom.remove(tinyMCE.activeEditor.dom.select('info'));
var a = ed.selection.getNode();
var txt = ed.selection.getContent();
var newT = document.createTextNode(txt);
a.parentNode.replaceChild(newT, a);
I'm using CKEditor in my web app, and I'm at a loss as to how to get the contents of the editor with HTML formatting.
var objEditor = CKEDITOR.instances["sectionTextArea"];
var q = objEditor.getData();
This will get me the text entered in CKEditor, without any markup.
However,
var q = objEditor.getHTML();
will return a null value. What am I doing wrong?
getHTML isn't a method of a CKEditor object, so instead of null you should have a javascript error.
The method defined by the api is getData() if that doesn't work then you have some other problem in your code, try to use an alert to verify the contents at that moment.
just to know that the right method for this is getData() didn't help me. I did not know how to use it on the CKEditor object. and CKEDITOR.getData() doesn't work.
this is how getData() is used on the CKEDITOR object:
CKEDITOR.instances.my_editor.getData()
...where my_editor is the id of your textarea used for CKEditor.
The opposite of it is setData():
CKEDITOR.instances.my_editor.setData("<p>My Text</p>");
To get htmlData from editor you should use the code snippet bellow:
var htmldata = CKEDITOR.instances.Editor.document.getBody().getHtml();
If this solution won't work, check if you have BBCode plugins uninstalled.
Please update ckeditor config.js with the following line
config.fullPage = true;
This will return the full html when you request getData();
This worked for me:
CKEDITOR.instances["id"].getData()
I am using the preview plugin to get the full HTML content, hope it helps.
CKEDITOR.getFullHTMLContent = function(editor){
var cnt = "";
editor.once('contentPreview', function(e){
cnt = e.data.dataValue;
return false;
});
editor.execCommand('preview');
return cnt;
}
in ckeditor 5, you can get the html data with editor.getData()
here is an example:
ClassicEditor
.create( document.querySelector( '#editor' ) )
.then( editor => {
console.log(editor.getData());
} )
.catch( error => {
console.error( error );
} );
For Java Users...
After pressing the submit button, the request goes by HTTP Post method. This Post request also contains the formatted html in the parameter named using the name attribute of the textarea.
So, if your textarea is something like...
<form method="post" action="createPDF.do"> <textarea name="editor1" id="editor1"/>
<input type="submit"/> </form>
Then, after pressing the submit button, you can get the formatted html in your servlet/controller by :
String htmlContent = request.getParameter("editor1");
You can also pass this variable containing the formatted html ('htmlContent') to ITEXT (or some other pdf converters) to create the pdf...
I realize this is old, but I had trouble finding an answer that made sense and returned the actual HTML, including images. If your ckeditor instance is attached to a textarea, you can simple get the value of the textarea to get the HTML.
For instance, if you're using jQuery:
$('#my_editor').val()
No need to go digging through the API.
If you have two CKEditor, you can use code bellow:
HTML
<textarea name="editor1"></textarea>
<textarea name="editor2"></textarea>
JS
CKEDITOR.replace( 'editor1' );
CKEDITOR.replace( 'editor2' );
var objEditor1 = CKEDITOR.instances["editor1"];
alert(objEditor1.getData()); // get html data
var objEditor2 = CKEDITOR.instances["editor2"];
alert(objEditor2.getData()); // get html data
Online Demo (jsfiddle)
I used the insert media feature in editor and .getData() did not return the required HTML to show the video thumbnail icon. The following worked for me to get the final HTML:
$(".ck-content").html()
Try this:
CKEDITOR.instances.YOUREDITOR.element.getHtml();
with CKEDITOR.instances.YOUREDITOR.element you select a DOM element and if you use
CKEDITOR.instances.YOUREDITOR.element.getHtml();
you can get all html from editor element.