You are here: Reference > JavaScript > client-side > HTML DOM > properties > previousSibling

previousSibling property

Browser support:
Returns a reference to the previous node of the current element's parent.
In other words, the previousSibling property returns the element that immediately precedes the current element in the childNodes collection of the current element's parent.

Syntax:

object.previousSibling;
You can find the related objects in the Supported by objects section below.
This property is read-only.

Possible values:

Returns a reference to the previous child or null if it does not exist.
Default: this property has no default value.

Example HTML code 1:

This example illustrates the use of the previousSibling property:
<head>
    <script type="text/javascript">
        function GetNextNode () {
            var parentElem = document.getElementById ("myDiv");

            secondElem = parentElem.childNodes[1];
            alert ("The value of the second node: " + secondElem.innerHTML);

            prevElem = secondElem.previousSibling;
            alert ("The value of the previous node: " + prevElem.innerHTML);
        }
    </script>
</head>
<body>
    <div id="myDiv"><div>first elem</div><div>second elem</div></div>
    <button onclick="GetNextNode ()">Get previous node!</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content