Key events not working for multiple ckeditors (Updated)

I have jsfiddle. Here the filter word is communicated to the user when he types it on ckeditor. In my example, filtered words are ants and words. So if you enter these words, it will warn the user.

HTML

<input type="textarea" id="editor1"/>
<div id="dest"></div>

Js

var filter = ['ants', 'words'], // our list of words
    regAry = new Array(), // we'll create one regex per word for testing
    alertedWords = new Array(), // keep track of how many words there were at the last alert, for each word
    reg = new RegExp("(/s" + filter.join("|") + "/s)", "g"); // one regex to rule them all!

for(var f in filter) { // setup...
    regAry[f] = new RegExp(filter[f], "g"); // one regex per word for testing
    alertedWords[f] = 0; // no alerts yet, so 0 for each word
}
var editor = CKEDITOR.replace( 'editor1' );
//var value = CKEDITOR.instances['editor1'].getData();
//alert(value);

 editor.on('contentDom', function() {
 editor.document.on('keyup', function(event) {

for(var index in regAry) { // loop over our list of words
    var value = CKEDITOR.instances['editor1'].getData();
       var test = value.match(regAry[index]); // test how many times this word appears
       if( test && test.length > alertedWords[index] ) // if it appears more than the last time we alerted...
       {
           alert("The following word/words  "+ CKEDITOR.instances['editor1'].getData().match(regAry[index])+" is banned"); // do an alert!
       }
        alertedWords[index] = (test ? test.length : 0); // update the word count for this word
    } // keep looping     

 });
 });

Now my problem arises if I have 2 or more ckeditors such as this This seems to not work. Although editors appear, warnings are not displayed.

HTML

<input type="textarea" id="editor1"/>
<input type="textarea" id="editor2"/>
<div id="dest"></div>

Js

var filter = ['ants', 'words'], // our list of words
    regAry = new Array(), // we'll create one regex per word for testing
    alertedWords = new Array(), // keep track of how many words there were at the last alert, for each word
    reg = new RegExp("(/s" + filter.join("|") + "/s)", "g"); // one regex to rule them all!

for(var f in filter) { // setup...
    regAry[f] = new RegExp(filter[f], "g"); // one regex per word for testing
    alertedWords[f] = 0; // no alerts yet, so 0 for each word
}

for(var i=1;i<3;i++){
var editor = CKEDITOR.replace( 'editor'+i );
//var value = CKEDITOR.instances['editor1'].getData();
//alert(value);

 editor.on('contentDom', function() {
 editor.document.on('keyup', function(event) {

for(var index in regAry) { // loop over our list of words
    var value = CKEDITOR.instances['editor'+i].getData();
       var test = value.match(regAry[index]); // test how many times this word appears
       if( test && test.length > alertedWords[index] ) // if it appears more than the last time we alerted...
       {
           alert("The following word/words  "+ CKEDITOR.instances['editor'+i].getData().match(regAry[index])+" is banned"); // do an alert!
       }
        alertedWords[index] = (test ? test.length : 0); // update the word count for this word
    } // keep looping     

 });
 });
}

Plz will suggest me what to do.

+4
source share
2 answers

i. (contentDom, keyup), i 3. CKEDITOR.instance("editor3"), . , i :

for(var i=1; i<3; i++){
   addListeners(i);
}
function addListeners(i){
   //body of for loop
}

DEMO

0

for, jquery each, :

$( 'input[type=textarea]').each( function(indx) {

  var editor = CKEDITOR.replace( $(this).attr('id') );

  .....

FIDDLE

+1

Source: https://habr.com/ru/post/1536901/


All Articles