Xui.js .children() alternative

365 Views Asked by At

What's the xui.js (or pure javascript if necessary) alternative for jQuery's .children().

To be more specific, I'm trying to get the child of $x(this) but $x(this).children('p') doesn't work (as in, it doesn't seem to exist in the library. It's not recognized according to Firebug nor is it in the xuijs docs).

1

There are 1 best solutions below

0
On

Not sure about that library, but if you have the native DOM element, the native JS could look like this:

[].filter.call(this.children, function(el, i) { 
    return el.nodeName.toLowerCase() === 'p'; 
});

If you're supporting legacy browsers, you can get a shim for Array.prototype.filter from MDN.

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter#Compatibility


And you can easily make a reusable function too.

function children(elem, s) {
    return [].filter.call(elem.children, function(el, i) { 
        return el.nodeType === 1 && (!s || el.nodeName.toLowerCase() === s); 
    });
}

children(this, 'p');

It only filters by tag name, but it would be simple to extend it.