I am looking for a way to make CKEDITOR wysiwyg interactive content. This means, for example, adding some onclick events to specific elements. I can do something like this:
CKEDITOR.instances['editor1'].document.getById('someid').setAttribute('onclick','alert("blabla")');
After processing this action, it works well. But, therefore, if I switch the mode to its original mode and then return to the wysiwyg mode, javascript will not work. The onclick action is still displayed in native mode, but not displayed inside the textarea element.
However, it is interesting that this works great every time:
CKEDITOR.instances['editor1'].document.getById('id1').setAttribute('style','cursor: pointer;');
And it also does not appear inside the textarea element! How is this possible? What is the best way to work with onclick and onmouse events of CKEDITOR content elements?
I tried to manually write this in source mode:
<html> <head> <title></title> </head> <body> <p> This is some <strong id="id1" onclick="alert('blabla');" style="cursor: pointer;">sample text</strong>. You are using <a href="http://ckeditor.com/">CKEditor</a>.</p> </body> </html>
And Javascript (onclick action) is not working. Any ideas?
Thanks a lot!
My final decision:
editor.on('contentDom', function() { var elements = editor.document.getElementsByTag('span'); for (var i = 0, c = elements.count(); i < c; i++) { var e = new CKEDITOR.dom.element(elements.$.item(i)); if (hasSemanticAttribute(e)) { // leve tlacitko mysi - obsluha e.on('click', function() { if (this.getAttribute('class') === marked) { if (editor.document.$.getElementsByClassName(marked_unique).length > 0) { this.removeAttribute('class'); } else { this.setAttribute('class', marked_unique); } } else if (this.getAttribute('class') === marked_unique) { this.removeAttribute('class'); } else { this.setAttribute('class', marked); } }); } } });