If you do not care at all about supporting Internet explorer, there is a new element method called closest which can be used as a way to get the closest element that matches a given query string. It then goes without saying that this method is yet another tool in the toolbox when it comes to this sort of thing, and it is also a little more versatile beyond just getting the parent element of an element. There are pollyfills, but it might still be best to just use the older properties if you just simply want to get the parent element and not much of anything else.
I might also touch base on some other related topics as well when it comes to a chain of elements from document up to a given element as well, that is getting all parent elements of a given node. There is also of course the various options that can be used to get an element reference to begin with also, and the fact that these options are also a way of getting a parent element reference and references in general. So there is a bit of ground to cover when it comes to getting really into what all the options are, however I will be keeping the basic stuff that will work well at the top of this post.
First off there are the el.parentElement, and el.parentNode properties that come to mind when it comes to the first solution for this sort of thing. So the basic process is to first get a reference to the element to which I want to find the parent for, any method for doing so will work find for that such as document.getElementById or document.querySelector. Once that has been obtained the parentElement or parentNode properties of an element of interest will refer to the parent element of that element if it has one.
So then in this section I will just be getting to some quick simple examples of these options for getting a reference to a parent element or node in general. I also often use this section to go over any additional topics of interest before reading the rest of the post.
Once a reference to an element is gained by use of a method like getElementById or querySelector, there is the parentElement property of the element reference if it has one. This property is there to get a parent DOM element rather than a node in general, as there are some kinds of nodes that are often used that are not really actual html elements, such is the case with things like SVG. Tis then is the main different between parent element and parent node, the node property is more general and should work with nodes in general and not just standard html elements that are part of an html spec.
In the event that the element does not have a parent element, or the parent element is not a DOM element the property will have a value of null. This might be desired in some cases as the value of null will evaluate to false and can then be used as a way to break out of looping for example.
In the event that the node is not a DOM node it will return null, this would make sense because the name of the property is indeed parent element, so if the parent is NOT A DOM Element, but some other kind of node then it should return false. If for some reason I want to use another property that will work with nodes in general then there is the parent node property which might be a more robust option for this sort of thing.
Another option is the parent node property, this works in more or less the same way as parentElement but with one note worthy difference. If there is a non DOM element node of sorts it will give that in place of what would otherwise be a null value when using the parent element property.
So a modern option now is the closest element method that works on most modern browsers. One draw back though is that it will get the closest element that matches the query string including the element from which it is called. So it may not work out so well when it comes to getting the parent element depending on the situation, and how it is used. However I think that it is of course work mentioning here in this post, as the method strikes me as a more flexible way of getting elements near a target element of interest. There is of course going beyond just getting that parent element, and being able to get any element that meets a given query that is near a given element regardless if it is a parent or a child element.
So for now I am not aware of any native browser method that can be used to get all the parent elements of a given element, but it is not to hard to write one. Once way to do so would be to write a method that will take a child element, and a function to call for each parent until a return value of the function is true. So in this section I will be going over the source code of a loop parents method, and a few use case examples of such a method.
First off here is an example that makes use of a loop back method that I have made that is based off of an example that I found while doing research for this post. The method works by taking a child element reference as the first argument, and then it will fire a for parent method for each parent node until the return value of the for parent method is true, or until a null value is reached for a parent node value.
This method can then be used in a whole range of different ways depending on what it is that I want to get when it comes to looping down the list of parent elements. In this example alone I have two use case examples that are doing to separate things one of which is looping back all the way and logging the results to the console, the other is stopping when the body element is reached. With that said the rest of this section will just be different use case examples of this method, and using it to create additional methods for specific situations.
Now that is have a decent loop parents method I can use this method to create another method that will return an array of all parent elements. This can be done by just creating a new array in the body of a function that will take a single element as an argument that is the child element to loop back from. I then call my loop parents method passing this child argument as the first argument for that method, and in the for parent method I just push the elements into the array. Once done I just return the array that will contain all the parent elements. After that I can use any additional array prototype method to filter, or preform some kind of action for each parent element.
I can use this loop parents method to create an array of all the parents if I want to. However what I really have in mind with this is to have a method where I loop back until I find what I want. Often I might just be looping for one parent element that meets a given condition rather than building an array of elements without any additional conditions other than it just being an element or node in general. So then with that said maybe at least a few more examples of this method are called for.
In this example I am not once again using the loop parents method to loop over all the parent nodes from a starting child node. This time I am looking for a parent node that has a given tag name such as a div element. So when I call the loop parents method I am looking at the node name property of each parent node, and in the event that the parent node name is the name I am looking for that parent node will be returned. The default value for the parent is the null value, and this is what will be returned in the event that there is no match for the given tag name.
Making a variation of this that looks at the class name property would not be so hard as well. In such a case I would want to do more or less the same thing that I am doing here only I would be looking at the class name property of each parent element.
So a related topic of interest when it comes to getting parent elements is the subject of event bubbling, when it comes to working with event handlers, and the various properties of the event objects that can be worked with inside of such event handlers. When an element is clicked for example it will fire an on click event that is set for that element, but it will also bubble up to the top most parent element and fire event handlers all the way up unless this is stopped. So then it is possible to set an event listener for a parent element, and then if any child element is clicked that event handler will fire. Inside the body of the event handler the target property of the event object should refer to the child element, and the current target property should refer to the parent element. So then in this section I will be going over some code examples that explore this related topic of interest when it comes to getting references to one or more parent elements.
To start this off just take a moment to look at a fairly simple example of what this is all about. Say I attach and event handler to a parent element such as the body element of a page for the click event. Whenever any element in the body of the document is clicked, that will result in the event handler firing. Inside the body of this event hander I can access the event object, and it is the target, and current target properties of this event object that are of interest.
So when it comes to event handers the target property of the event object will refer to the element where the event happened and the current target property will refer to the element where the event handler is attached. In most cases I would assume then that the current target property of the event object would be the parent, and the target property would be the child.
Now for a more advanced example of this that may help to really show how these properties are helper when it comes to attaching just a single event handler to a parent element. This time I am making a single for parent child method that can take a for child method, a for parent method, and a for only array of string values that can be used to set what child elements to call the for child method for for each client event that happens for a parent element and all children of the parent to which this method is used with.
When calling this for parent child method the first argument is a parent element, or a query string that can be used to get a parent element to attach a main event hander to. The second argument is then an options object that can contain an array of string values that can be used to find out if a for child method should be called or not. With that said this options object can also be used to set what the for child method is, and a for parent method can also be attached that will always fore for each event.
When this example is up and running when I click any child of the main warp div that I attached to that is a div that applies to any of the rules set with the for only array the color of that div will change. When I click a div that is a child that does not apply to any of the roles then the for child method will not fire. Also I made it so that when the canvas element is clicked I draw circles to it.
So maybe I went a little overboard with this example, I then to do that now and then, but I wanted to have at least one examples that helps to really showcase why something like this is helpful when working out certain kinds of system where I want to get references to parent and child elements when some kind of event happens.
As of this writing there is no css selector that I know of that can be used to get a parent element, so there is no way of getting a parent element with querySelector. There is of course chatter about possible future selectors and pseudo classes that might be a way to do so, but so far nothing solid or well supported.
So getting the parent element of an html element reference is just a matter of choosing an option for doing so. There are two main properties that come to mind one of which will work with just html elements, and the other will work with nodes in general for the most part. There are many other ways of doing so, but for the most part there are just those two properties that can be used to just do so and move on.
In some cases I might want to do something that is a little more advanced and for that it is not so hard to use the fact that these properties return null as a way to get out of a loop where I just keep setting the current value of a node variable to the value of of the previous nodes parent node property. There is then making a simple loop parents method that will take a function to call for each parent method, and then this kind of method can be used in all kinds of different ways to address just about any issue that might come up when it comes to the need to looking into parent elements.