Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TinyMCE - Insert Content at the bottom

I know we can insert the code to the top this way.

ed.selection.setCursorLocation(ed.getBody().firstChild, 0);

But, I am not sure how to implement this for content at the bottom.

like image 653
TonyTakeshi Avatar asked Nov 01 '11 04:11

TonyTakeshi


2 Answers

Another approach:

function getTextNodes(node, nodeType, result){

    var children = node.childNodes;
    var nodeType = nodeType ? nodeType : 3;

    var result = !result ? [] : result;
    if (node.nodeType == nodeType) {
        result.push(node);
    }

    for (var i=0; i<children.length; i++) {
        result = this.getTextNodes(children[i], nodeType, result)
    }

    return result;
};

// get all Textnodes from lastchild, calc length
var textnodes = getTextNodes(ed.getBody().lastChild);

// set Cursor to last position
ed.selection.setCursorLocation(textnodes[textnodes.length-1], textnodes[textnodes.length-1].textContent.length );
like image 141
Thariama Avatar answered Sep 28 '22 09:09

Thariama


You can use the following to DOMUtils.add() method to add a new HTML element to the document body.

For example, the following adds an empty paragraph with a CSS Class of "text_it"

ed.dom.add(ed.getBody(), 'p', {'class' : 'text_it'});
like image 41
Brett Henderson Avatar answered Sep 28 '22 07:09

Brett Henderson