previousElementSibling property
Retrieves a reference to the immediately preceding sibling element or null if the element does not have any preceding siblings.
![]() |
Syntax
| JavaScript | |
|---|
Property values
Type: Object
A reference to the immediately preceding sibling element.
Standards information
- Element Traversal Specification, Section 2.3
Examples
This example shows how to get the content of a list using firstElementSibling, nextElementSibling, previousElementSibling, and lastElementSibling.
<!DOCTYPE html>
<html>
<head>
<title>IElementTraversal Example</title>
<script>
function GetListItems () {
var list = document.getElementById ("girls"); // find our list
var results = document.getElementById("results"); // get our results line element
var oChild = list.lastElementChild; // start with the last item in list
while (oChild) { // get and display each item in list
results.innerHTML += "<br/>" + oChild.innerHTML;
oChild = oChild.previousElementSibling; // get previous element in list
}
}
function GetListItems2 () {
var list = document.getElementById ("girls"); // find our list
var results = document.getElementById("results"); // get our results line element
var oChild = list.firstElementChild; // start with the first item in list
while (oChild) { // get and display each item in list
results.innerHTML += "<br />" + oChild.innerHTML;
oChild = oChild.nextElementSibling; // get next element in list
}
}
function refresh()
{
window.location.reload( false ); //reload our page
}
</script>
</head>
<body>
<ol id="girls">
<li>Sugar</li>
<li>Spice</li>
<li>Everything nice</li>
</ol>
<p id="results">Girls have: </p>
<p>
<button onclick="GetListItems ();">Get list backwards</button>
</p>
<p>
<button onclick="GetListItems2 ();">Get list forwards</button>
</p>
<p>
<input type="button" value="Refresh page" onclick="refresh()" />
</p>
</body>
</html>
See also
- a
- abbr
- acronym
- address
- applet
- area
- attribute
- b
- base
- baseFont
- bdo
- big
- blockQuote
- body
- button
- caption
- center
- cite
- code
- col
- colGroup
- comment
- dd
- del
- dfn
- dir
- div
- dl
- dt
- em
- embed
- fieldSet
- font
- form
- frame
- frameSet
- head
- hn
- hr
- html
- i
- iframe
- img
- input type=button
- input type=checkbox
- input type=email
- input type=file
- input type=image
- input type=number
- input type=password
- input type=radio
- input type=range
- input type=reset
- input type=search
- input type=submit
- input type=tel
- input type=text
- input type=url
- ins
- kbd
- label
- legend
- li
- link
- listing
- map
- marquee
- menu
- nextID
- ol
- option
- p
- plainText
- pre
- q
- s
- samp
- script
- select
- small
- span
- strike
- strong
- sub
- sup
- table
- tBody
- td
- textArea
- tFoot
- th
- tHead
- title
- tr
- tt
- u
- ul
- var
- xmp
Show:
