Want to get the Sibling of a list element in JavaScript? Use the following example:
1 2 3 4 5 6 7 8 9 10 | <html> <body> <ul><li id="item1">Tesla</li><li id="item2">Spacex</li><li id="item3">Solarcity</li></ul> <p id="next"></p> <script> var x = document.getElementById("item1").nextSibling.innerHTML; document.getElementById("next").innerHTML = "The sibling element is "+" "+x; </script> </body> </html> |
Output
1 2 3 4 | Tesla Spacex Solarcity The sibling element is Spacex |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.