0

Currently in TinyMCE 4 I'm trying to add emoticons as buttons in my toolbar.

The code below works fine, I can see all buttons in the toolbar:

var array = ['applause', 'bumping', 'cool', 'cry', 'grin', 'happy', 'laughing', 'notalking', 'sad', 'wink'];

    for (var i in array) {

        editor.addButton(array[i], {
            title: '',
            icon: true,
            image: 'img/smileys/'+ array[i] +'.gif',
            onclick: function() {
                editor.execCommand('mceInsertContent', false, '<img src="img/smileys/'+ array[i] +'.gif">');
            }
        });

    }

However, the array[i] in the editor.execCommand line, always return its value as wink (last value of the array). Meanwhile, the other two instances of array[i] in the code works just fine.

user2094178
  • 9,204
  • 10
  • 41
  • 70

1 Answers1

1

Close over i

for (var i in array) {
  new function(){
    var iCopy = i;
    editor.addButton(array[iCopy ], {
        title: '',
        icon: true,
        image: 'img/smileys/'+ array[iCopy ] +'.gif',
        onclick: function() {
            editor.execCommand('mceInsertContent', false, '<img src="img/smileys/'+ array[iCopy ] +'.gif">');
        }
    });
  }
}
Travis J
  • 81,153
  • 41
  • 202
  • 273