Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting selected html content in tinymce editor

Tags:

get

tinymce

I have created a custom button using this code

    setup : function(ed) {
    ed.addButton('Tittle', {
                title : 'Tittle',
                image : './images/T.jpg',
                onclick : function() {
                ed.focus();
            var c = ed.selection.getNode().nodeName;
        if(c!="TITTLE")
        {
             ed.selection.setContent('<tittle>' + ed.selection.getContent() + '</tittle>');

        }
        else
        {

        }
}
        });

When a user select a text and click on the new button, i want to add a <title> tag at beginning and ending, if <tittle> tag is not their.If <tittle> tag is already their in the selected text i want to remove the tag

like image 317
Warrior Avatar asked Oct 21 '10 08:10

Warrior


People also ask

How do you get content in TinyMCE with HTML tags?

The TinyMCE getContent and setContent methods You can do this using the getContent() API method. Let's say you have initialized the editor on a textarea with id=”myTextarea”. This will return the content in the editor marked up as HTML.

How do I save HTML content of TinyMCE into a file?

To save the page, you have to insert TinyMCE in a HTML form and add a submit button. Then you'll have to handle the submitted form with a language like PHP. A call to the file_put_contents() function should do it.

How do I get the content of TinyMCE editor in jQuery?

You can use the activeEditor for that, or if (for some reason) you have the original element that created the editor in a jQuery object, you can use that jQuery object to get the id of the original element and use that in order to get the content of the TinyMCE (using the TinyMCE editor).


1 Answers

try

selection.getContent({format : 'text'});

or

selection.getContent({format : 'html'});

http://www.tinymce.com/wiki.php/API3:method.tinymce.dom.Selection.getContent

EDIT: To achieve what you want you could do:

if(c!="TITTLE") {

  node = ed.selection.getNode();

  with(document.getElementById(iframe_id).contentWindow){
      var newElement = document.createElement("tittle");
      newElement.innerHTML = node.innerHTML;
  }

  node.parentNode.replaceChild(newElement, node);

}
else {

  node = ed.selection.getNode();

  with(document.getElementById(iframe_id).contentWindow){
      var newElement = document.createTextNode(node.innerHTML);
  }

  node.parentNode.replaceChild(newElement, node);
}
like image 130
Thariama Avatar answered Oct 13 '22 23:10

Thariama