Select parent element with javascript (no Jquery)

Asked

Viewed 5,698 times

0

I have two elements and would like to select the parent element using Javascript only, someone knows some way?

<tr>
    <td id="filho"><td>
</tr>

2 answers

7


Utilize parentNode: Node.parentNode

var filho = document.getElementById("filho");
var pai = filho.parentNode;

console.log(pai);
<div id="pai">
  Sou Pai
  <div id="filho">
    Sou filho do meu pai :D
  </div>
</div>

1

Use the property parentNode

Ex:

document.getElementById('filho').parentNode

Browser other questions tagged

You are not signed in. Login or sign up in order to post.