Search code examples
jquerydomckeditor

CKEDITOR - Select span by its ID or CLASS and get its data attribute value


How do you manage the content in CKEditor with jQuery?

I have this HTML:

<div id="ckeditor_block">
   <textarea id="editor1">
      <span class="sub" id="sub1" data-time-start="0">Hello </span>
      <span class="sub" id="sub2" data-time-start="2">My </span>
      <span class="sub" id="sub3" data-time-start="6">Name </span>
      <span class="sub" id="sub4" data-time-start="8">Is </span>
      <span class="sub" id="sub5" data-time-start="12">Zoob</span>
   </textarea>                
</div>

My JS:

var textarea;

$(document).ready(function () {
    textarea = $('#ckeditor_block').find('textarea').attr('id');
    ckeditor_init();
});

function ckeditor_init() {
    CKEDITOR.replace(textarea, {
        language: 'fr',
        allowedContent: true
    });
}

The JSFIDDLE

Until this point, this works nice.

Now how can I simply, in the CKeditor iframe or whatever, select for example the span with the ID = sub3 and after that get its content (here : Name) and its attribute data-time-start (here : 6)?

Moreover, in a loop, how can I select each element with the class sub>

Something like this, but with method of CKEDITOR:

$.each($(CKEditor.getBody()).find(".sub"), function () { // How select SUB class name in CKEDITOR ??
     var d = $(this).attr("data-time-start"); // HOW SELECT FOR EACH 'SUB' HIS data-time-sart attribute in CKEDITOR ??
     $(".st").removeClass("active"); // HOW REMOVE CLASS FOR ALL class 'SUB' in CKEDITOR ??
     $(this).addClass("active"); // HOW ADD CLASS 'active' TO THIS 'SUB' in CKEDITOR ??    
});

I tried to get the content like that, but after. How do I do this?

 var editor = CKEDITOR.instances[textarea];
 var $dataCkeditor = editor.getData();

Solution

  • i clone ur project on my desktop, and it work for me! But in Fiddle IT does not work! Be careful! I dont know why, but in fiddle it just dont work, try that:

    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script src="http://ckeditor.com/apps/ckeditor/4.0/ckeditor.js"></script>
    <script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.0/js/bootstrap.min.js"></script>
    </head>
    <body>
    <div id="ckeditor_block">
        <textarea id="editor1"> <span class="sub" id="sub1" data-time-start="0">Hello </span>
         <span class="sub" id="sub2" data-time-start="2">My </span>
         <span class="sub" id="sub3" data-time-start="6">Name </span>
         <span class="sub" id="sub4" data-time-start="8">Is </span>
         <span class="sub" id="sub5" data-time-start="12">Zoob</span>
        </textarea>
    </div>
    <script type='text/javascript'>
    var textarea;
    $(document).ready(function () {
        textarea = $('#ckeditor_block').find('textarea').attr('id');
        CKEDITOR.replace(textarea, {
            language: 'fr',
            uiColor: '#9AB8F3',
            allowedContent: true,
            disallowedContent: 'script; *[on*]',
            enterMode: CKEDITOR.ENTER_BR,
            toolbar: [
                ['Bold', 'Italic', 'Underline', '-', 'TextColor', '-', 'RemoveFormat'],
                ['Cut', 'Copy', 'Paste', '-', 'Undo', 'Redo'],
                ['JustifyLeft', 'JustifyCenter', 'JustifyRight'],
                ['Find', 'Replace', 'SelectAll'],
                ['Source', '-', 'Maximize', '-', 'Save']
            ]
        });
        function waitInit(){
            var subs = $( CKEDITOR.instances.editor1.window.getFrame().$ ).contents().find('.sub' );
            $.each(subs, function(){
                $this = $(this);
                console.log("value = " + $this.text());
                console.log("time = " + $this.attr('data-time-start'));
            });
        }
        CKEDITOR.on('instanceReady', function(){ waitInit(); });
    });   
    </script>
    </body></html>
    

    It was 2 trouble - first on fiddle, and second - we call function before ckeditor initialize, i cant find callback for init, so input timeout with a little delay, for it, i test it - all works fine!