Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

js DOM siblings

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>JavaScript Siblings</title>
</head>
<body>
    <ul id="menu">
        <li>Home</li>
        <li>Products</li>
        <li class="current">Customer Support</li>
        <li>Careers</li>
        <li>Investors</li>
        <li>News</li>
        <li>About Us</li>
    </ul>
    
    <script>
        let getSiblings = function (e) {
            // for collecting siblings
            let siblings = []; 
            // if no parent, return no sibling
            if(!e.parentNode) {
                return siblings;
            }
            // first child of the parent node
            let sibling  = e.parentNode.firstChild;
            // collecting siblings
            while (sibling) {
                if (sibling.nodeType === 1 && sibling !== e) {
                    siblings.push(sibling);
                }
                sibling = sibling.nextSibling;
            }
            return siblings;
        };

        let siblings = getSiblings(document.querySelector('.current'));
        siblingText = siblings.map(e => e.innerHTML);
        console.log(siblingText);
    </script>
</body>
</html>
Code language: HTML, XML (xml)
Comment

PREVIOUS NEXT
Code Example
Javascript :: react native text align vertical center 
Javascript :: angularjs 1.5.6 cdn 
Javascript :: javascript como recorrer un array multidimensional 
Javascript :: socketio connect websockets 
Javascript :: check valid Phonenumbers 
Javascript :: angualar image upload service 
Javascript :: how to export module in node js 
Javascript :: React-redux and redux 
Javascript :: Multiple Slick Sliders On Same Page with same classes 
Javascript :: ssr full form in nextjs 
Javascript :: How To Take Screenshots In The Browser Using JavaScript 
Javascript :: type of angular 
Javascript :: javascript how to do else if 
Javascript :: get all child element of class javascript 
Javascript :: next js notifications 
Javascript :: javascript add id to element with class 
Javascript :: discord.js random output 
Javascript :: append textarea jquery with value 
Javascript :: command to run nextjs project 
Javascript :: js local file read to blob variable 
Javascript :: popup javascript 
Javascript :: The element.InnerHTML Property 
Javascript :: post to /wp-json/wp/v2/media 
Javascript :: Javascript - find the largest 
Javascript :: constructor function 
Javascript :: curl send json as variable 
Javascript :: load more button javascript 
Javascript :: put logo in qr code in react js qrcode.react 
Javascript :: cheerio example 
Javascript :: react router changing url but not rendering 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =