L
L
Lolo Penguin2020-05-10 13:08:57
JavaScript
Lolo Penguin, 2020-05-10 13:08:57

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;
    }
}


...well, how do I get the name of the parent tag? Thanks to.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem Andreev, 2020-05-10
@Pibodi

node.parentNode.nodeName
Tried?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question