Last Updated: February 25, 2016
·
618
· searsaw

DOM Navigation without jQuery

Text nodes can be painful when navigating the DOM. Let's disregard them without jQuery!

function getNextSibling(el) {
    el = el.nextSibling;
    while (el) {
    if (el.nodeType !== 3) {
        return el;
    }
    el = el.nextSibling;
    }
}

This will let you get the next sibling of the element you grabbed without worrying about text nodes.

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside