Answer the question
In order to leave comments, you need to log in
How to get parent object tag of selected text?
Please tell me how to get the tag of the parent object of the selected text. I have codec:
let range = window.getSelection().getRangeAt(0);
parObj = getContainingP(range.commonAncestorContainer);
return alert(parObj ? parObj.id : 'null' );
function getContainingP(node) {
while (node) {
if (node.nodeType == 1 && node.tagName.toLowerCase() == "p") {
return node;
}
node = node.parentNode;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question