previousElementSibling property in javascript
previousElementSibling property is used to return the preivous element of the specified element.
It returns null if the specified element is the first one.
Syntax
var elem = element.previousElementSibling;
Example Explanation
- In the above example, when the button is clicked fun_name() function is called.
- In the Javascript function, first we are getting the element (document.getElementById("idP2")) and storing it in attr variable.
- Using the previousElementSibling property, we are getting the element previous to the specified element using its id idP1, and displaying it (alert (attr.previousElementSibling.innerHTML)).