Best way to get child nodes in JavaScript

The contains all nodes, including text nodes consisting entirely of whitespace

The cross browser way to do is to use to get , then make an array of all nodes with ELEMENT_NODE.

/** 
* Return direct children elements. 
* * @param {HTMLElement} 
* @return {Array}
*/

function elementChildren (el){

  var childNodes = el.childNodes, children =[], i = childNodes.length;

  while(i--){

    if(childNodes[i].nodeType ==1){ 
        children.unshift(childNodes[i]);
    }

  }

  return children;
}


Thanks

To view or add a comment, sign in

More articles by Mohammed Misyaath

  • Hash Indexes

    Hash index built on Has table and useful to lookup exact value. For each row; storage engine computes hash codes for…

  • MySQL B-tree Indexes

    B-tree indexes uses B-tree data structure, Most of the MySQL storage engine support this. MyISAM use prefix compression…

    1 Comment
  • Create go Function with reflect package

    Package reflect implements run-time reflection, allowing a program to manipulate objects with arbitrary types. Can…

  • Microservices: Event-driven Patterns

    Event driven microservices being applied to data pattern. Event driven microsercvice pattern requires one or more steps…

  • Microservices: Interservice Communication pattern

    Often you and yourself in a situation where you just need to push a message to remote system to do work and you don't…

  • Design Principals – Favour of composition over Inheritance

    Inheritance is main state of object oriented design and programming. with in inheritance we can avoid code duplication…

    1 Comment
  • Create Mongodb Replication

    Replica set in MongoDB is a group of mongod processes that maintain the same data. Replica set provides redundancy and…

  • Add PHP authentication on WP-Login.php in WordPress

    Add PHP HTTP authentication for WP-Login.php for prevent Brute force attack on site and access WP admin PHP HTTP…

  • Some htaccess hack for WordPress security

    Security in wordpress is taken very seriously by WordPress Core team. But with any other system they are potential…

Explore content categories