Reputation: 3792
I am working in CKEditor right now.And I have a Question for You guys.
I am Selecting particular text in CKEditor's Text area and getting the selected
nodes HTML content Using the Below code.
var editor = CKEDITOR.instances.editor1;
var sel = editor.getSelection();
sel.selectElement(sel.getStartElement());
var ranges = sel.getRanges();
var el = new CKEDITOR.dom.element("div");
for (var i = 0, len = ranges.length; i < len; ++i) {
el.append(ranges[i].cloneContents());
}
alert(el.getHtml());
The following returuns the currently selected text HTML content.
alert(el.getHtml());
My Question is How I get the selected node's parent tag?
Example
Example Word is,
<p>hi<b>welcome</b>world<p>
My Selection is,
<b>welcome</b>
How do i get the below parent tag.
<p></p>
Upvotes: 2
Views: 2414
Reputation: 3792
i find the answer.
//Get range
range = xhtmlCKEditor.createRange();
range.setStart(anchors[0].tag , 0 );
range.setEnd(anchors[0].tag.getLast(), 1 );
var firstNode = range.startContainer.getParent();
var lastNode = range.endContainer.getParent();
if(lastNode.type === CKEDITOR.NODE_ELEMENT && lastNode.getName() === "span")
{
range.setEndAfter(lastNode);
}
//Make end Get full if is tcElement
if(firstNode.type === CKEDITOR.NODE_ELEMENT && firstNode.getName() === "p")
{
range.setStartBefore(firstNode);
}
xhtmlCKEditor.getSelection().selectRanges([range]);
Then print the Selection it will get the html With parent tag.
Upvotes: 2