Javascript wrap text with tag

I am adding a button for tinyMCE, and I want to know how, for example, to wrap text inside tags using javascript (this selected text is wrapped inside tags [highlight][/highlight]). and now all tinymce

(function() {
tinymce.create('tinymce.plugins.shoutButton', {
    init : function(ed, url) {
        ed.addButton('shout.button', {
            title : 'shout.button',
            image : 'viral.gif',
            onclick : function() {
                window.alert("booh");
        });
    },
    createControl : function(n, cm) {
        return null;
    },
    getInfo : function() {
        return {
            longname : "Shout button",
            author : 'SAFAD',
            authorurl : 'http://safadsoft.com/',
            infourl : 'http://safadsoft.com/',
            version : "1.0"
        };
    }
});
tinymce.PluginManager.add('shout.button', tinymce.plugins.ShoutButton);

}) ();

0
source share
2 answers

The problem was syntax errors, not the correct closed brackets and some missing half colonies, with the help of the amazing Jsfiddle JSHint and JSLint, I fixed it:

(function () {
    tinymce.create('tinymce.plugins.shoutButton', {
        init: function (ed, url) {
            ed.addButton('shout.button', {
                title: 'shout.button',
                image: 'viral.gif',
                onclick: function () {
                    window.alert("booh");
                }
            });
            createControl: function (n, cm) {
                return null;
            }
            getInfo: function () {
                return {
                    longname: "Shout button",
                    author: 'You !',
                    authorurl: 'http://example.com/',
                    infourl: 'http://example.com/',
                    version: "1.0"
                };
            }
        }
    });
    tinymce.PluginManager.add('shout.button', tinymce.plugins.ShoutButton);
})();

Regards

+1
source

setSelectionRange (mozilla/webkit) selection.createRange(IE), . jsfiddle, , . , , URL- YouTube.

, , , , idPattern.exec().

idPattern = /(?:(?:[^v]+)+v.)?([^&=]{11})(?=&|$)/;
  //  var vidId = prompt("YouTube Video", "Enter the id or url for your video");


    var vidId;
      el = document.getElementById('texty');
      if (el.setSelectionRange) {
        var vidId = el.value.substring(el.selectionStart,el.selectionEnd);
      }
      else if(document.selection.createRange()) {
          var vidId = document.selection.createRange().text; 
      }

   alert(vidId);

EDIT: .

el = document.getElementById('texty');
  if (el.setSelectionRange) {

     el.value = el.value.substring(0,el.selectionStart) + "[highlight]" + el.value.substring(el.selectionStart,el.selectionEnd) + "[/highlight]" + el.value.substring(el.selectionEnd,el.value.length);

  }
  else if(document.selection.createRange()) {
      document.selection.createRange().text = "[highlight]" + document.selection.createRange().text + "[/highlight]"; 
  }
+2

All Articles