Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get elements just 1 level below the current element by javascript

Tags:

javascript

dom

I need to access the DOM tree and get the elements just 1 level below the current element.

Read the following code:

<div id="node">
    <div id="a">
        <div id="aa">
            <div id="ab">
                <div id="aba"></div>
            </div>
        </div>
    </div>
    <div id="b">
        <div id="ba">
            <div id="bb">
                <div id="bba"></div>
            </div>
        </div>
    </div>
    <div id="c">
        <div id="ca">
            <div id="cb">
                <div id="cba"></div>
            </div>
        </div>
    </div>
</div>

I want to get the 3 elements "a", "b", "c" under "node". What should I do?

var nodes = node.getElementsByTagName("div") <---- I get all the divs but not the 3 divs I need.

var nodes = node.childNodes; <---- works in IE, but FF contains Text Node

Does anyone know how to solve the problem?

like image 333
Billy Avatar asked Feb 27 '09 06:02

Billy


People also ask

How do you get the first element of getElementsByClassName?

If you want only the first element in the DOM with that class, you can select the first element out of the array returned. var elements = document. getElementsByClassName('className'); var requiredElement = elements[0];

What is $() in JavaScript?

The $() function The dollar function, $(), can be used as shorthand for the getElementById function. To refer to an element in the Document Object Model (DOM) of an HTML page, the usual function identifying an element is: document. getElementById("id_of_element").

How do I get next element in DOM?

The article Whitespace in the DOM contains more information about this behavior. You can use Element. nextElementSibling to obtain the next element skipping any whitespace nodes, other between-element text, or comments.

Can we get element by class?

The getElementsByClassName() method returns a collection of elements with a specified class name(s). The getElementsByClassName() method returns an HTMLCollection. The getElementsByClassName() property is read-only.


2 Answers

You could use a function that rules out all non-element nodes:

function getChildNodes(node) {
    var children = new Array();
    for(var child in node.childNodes) {
        if(node.childNodes[child].nodeType == 1) {
            children.push(child);
        }
    }
    return children;
}
like image 165
Turismo Avatar answered Oct 07 '22 22:10

Turismo


I'd highly recommend you look at JQuery. The task you're looking to do is straightforward in pure Javascript, but if you're doing any additional DOM traversal, JQuery is going to save you countless hours of frustration. Not only that but it works across all browsers and has a very good "document ready" method.

Your problem solved with JQuery looks like:

$(document).ready(function() {
    var children = $("#node").children();
});

It looks for any element with an id of "node" then returns its children. In this case, children is a JQuery collection that can be iterated over using a for loop. Additionally you could iterate over them using the each() command.

like image 33
Soviut Avatar answered Oct 07 '22 23:10

Soviut