<html>
<body>
<p>Example list:</p>
<ul id="myList"><li id="item1">Coffee</li><li id="item2">Tea</li></ul>
<p id="demo">Click the button get the id of the previous sibling of the second list item</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
var itm=document.getElementById("item2");
var x=document.getElementById("demo");
x.innerHTML=itm.previousSibling.id;
}
</script>
<p><strong>Note:</strong> Whitespace inside elements is considered as text, and text
is considered as nodes.</p>
<p>Try adding whitespace between the two &lt;li&gt; elements, and the result will be "undefined".</p>
</body>
</html>