How to check if parent contains in html using javascript

I need to check if the parent is a span. How can I do this using javascript (not jquery)?

+6
source share
3 answers
element.parentNode.tagName.toLowerCase() == 'span' 
+10
source
 var elem = document.getElementById("myElement"), isParentSpan = elem.parentNode.tagName === "span"; alert( isParentSpan ); 
+4
source

Remember that .nodeName returns the UPPERCASE string (with some exceptions). It is safe for toLowerCase () when you are comparing.

http://ejohn.org/blog/nodename-case-sensitivity/

 var el = document.getElementById('test'), parent = el.parentElement || el.parentNode, parentType = el.parentElement.nodeName.toLowerCase(); if ( 'span' === parentType ) { alert('Parent is a span!'); } 

http://jsfiddle.net/TT6jr/

+3
source

Source: https://habr.com/ru/post/927791/


All Articles