JavaScript

How to Access to Parent of “this” in JavaScript?

While appending various functionalities in a web page or the site, there can be a requirement to inter-relate various functionalities. For instance, invoking a particular element by its relative elements or overviewing the child elements corresponding to a specific parent. In such situations, accessing the parent of “this” in JavaScript assists in relating the added features.

This article will describe the approaches to accessing the parent of “this” in JavaScript.

How to Access the Parent of “this” in JavaScript?

To access to parent of “this” in JavaScript, apply the following approaches:

Method 1: Access to the Parent of “this” in JavaScript Using the parentElement and nodeName Properties

The “parentElement” property fetches the parent element of the specified element, and the “nodeName” property displays the node name. These properties can be utilized to access the node name of the parent element corresponding to the fetched element.

Example

The below-stated example explains the stated concept:

<h3>Parent Node

<br><strong id="myChild">Child Node</strong>

</h3>

<p>Click the button to see the Parent Node Element</p>

<button onclick="myFunction()">Parent Node</button>

<script>

function myFunction() {

this.x = document.getElementById("myChild").parentElement.nodeName;

alert('The Heading of Parent Node is : ' + x)

}

</script>

In the above code lines:

  • Include an “<h3>” element as a parent node and allocate the “<strong>” element as a child node having the stated “id”.
  • In the next step, create a button that invokes the function “myfunction()” using the “onclick” event.
  • In the JavaScript part of the code, define a function named “myfunction()”.
  • In the function definition, “this” object refers to the global object and points to the accessed element via the “getElementById()” method.
  • The “parentElement” property gets the parent element corresponding to the fetched element, and the “nodeName” returns the node name corresponding to the parent element.
  • Lastly, display the parent node name via an alert dialogue box.

Output

In the output, it is notified that the node name of the parent element is displayed.

Method 2: Access to Parent of “this” in JavaScript Using parentNode and classList Properties

The “parentNode” property is used to return the parent node of the element, and the “classList” property returns the class names of an element. These approaches can be implemented to return the class name of the first parent corresponding to the fetched element.

Example

Let’s overview the below-stated example:

<div class="myParent">

<h3 id="myChild">This is Linuxhint Website</h3>

</div>

<script>

this.myChild =document.getElementById('myChild');

this.x = myChild.parentNode;

console.log('Class name of parent element is : ', x.classList[0]);

</script>

In the above code block:

  • Likewise, allocate the parent and child elements having the stated attributes.
  • In the JavaScript code, the “getElementById()” method is used to access the child element “<h3>” by its “id” using “this” object, respectively.
  • In the next step, another “this” object points to the parent node of the fetched element and accesses it via the “parentNode” property.
  • Lastly, display the first class name corresponding to the parent element via the “classList” property.

Output

In this particular output, the class name of the parent element is returned.

Conclusion

To access the parent of “this” in JavaScript, apply the combined “parentElement” and “nodeName” properties or the “parentNode” and “classList” properties. The former approaches can be implemented to return the node name of the parent element corresponding to “this” object. The latter approach can be utilized to access the first class name of the parent element accordingly. This blog discussed the approaches to access the parent of “this” in JavaScript.

About the author

Zahra Zamir

An Electronics graduate who loves to learn and share the knowledge, my passion for my field has helped me grasp complex electronics concepts and now I am here to share them with others.