HTML How to get id of parent Component?

RAVI picture RAVI · May 1, 2012 · Viewed 7.4k times · Source

I would like to get the id of the parent tag using javascript. In this example, the parent id of the text "stackoverflow" is "sofsite" and the parent id of "This" is "sofbody".

 <body id = 'sofbody'>
      This is <a href = "www.stackoverflow.com" id = "sofsite">stackoverflow</a>.
 </body>

Answer

Bergi picture Bergi · May 1, 2012
var parentid = textnode.parentNode.id;

See docs for parentNode.