Javascript DOM HTML Node nextSibling Property get next element

Description

Javascript DOM HTML Node nextSibling Property get next element

View in separate window

<!DOCTYPE html> 

<html lang="en"> 
<head> 
    <title></title> 
</head> //  w w w.  j a va 2 s .  c o m
<body> 
    <h1 id="heading1">My Heading</h1> 
    <p id="paragraph1">This is some text in a paragraph</p> 
    <script> 
        let h1Element = document.getElementById("heading1"); 
        h1Element.style.color = "red"; 

        let pElement; 

        if (h1Element.nextSibling.nodeType == 1) { 
            pElement = h1Element.nextSibling; 
        } else { 
            pElement = h1Element.nextSibling.nextSibling; 
        } 
        pElement.style.color = "red"; 
    </script> 
</body> 
</html> 



PreviousNext

Related