Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have to check that the nodeType == 1.</p> <pre><code>if (el.nodeType === 1) { return el; } </code></pre> <p>I wrote a small DOM traversing class for ya (mostly copied from MooTools). Download here: <a href="http://gist.github.com/41440" rel="nofollow noreferrer">http://gist.github.com/41440</a></p> <pre><code>DOM = function () { function get(id) { if (id &amp;&amp; typeof id === 'string') { id = document.getElementById(id); } return id || null; } function walk(element, tag, walk, start, all) { var el = get(element)[start || walk], elements = all ? [] : null; while (el) { if (el.nodeType === 1 &amp;&amp; (!tag || el.tagName.toLowerCase() === tag)) { if (!all) { return el; } elements.push(el); } el = el[walk]; } return elements; } return { // Get the element by its id get: get, walk: walk, // Returns the previousSibling of the Element (excluding text nodes). getPrevious: function (el, tag) { return walk(el, tag, 'previousSibling'); }, // Like getPrevious, but returns a collection of all the matched previousSiblings. getAllPrevious: function (el, tag) { return walk(el, tag, 'previousSibling', null, true); }, // As getPrevious, but tries to find the nextSibling (excluding text nodes). getNext: function (el, tag) { return walk(el, tag, 'nextSibling'); }, // Like getNext, but returns a collection of all the matched nextSiblings. getAllNext: function (el, tag) { return walk(el, tag, 'nextSibling', null, true); }, // Works as getPrevious, but tries to find the firstChild (excluding text nodes). getFirst: function (el, tag) { return walk(el, tag, 'nextSibling', 'firstChild'); }, // Works as getPrevious, but tries to find the lastChild. getLast: function (el, tag) { return walk(el, tag, 'previousSibling', 'lastChild'); }, // Works as getPrevious, but tries to find the parentNode. getParent: function (el, tag) { return walk(el, tag, 'parentNode'); }, // Like getParent, but returns a collection of all the matched parentNodes up the tree. getParents: function (el, tag) { return walk(el, tag, 'parentNode', null, true); }, // Returns all the Element's children (excluding text nodes). getChildren: function (el, tag) { return walk(el, tag, 'nextSibling', 'firstChild', true); }, // Removes the Element from the DOM. dispose: function (el) { el = get(el); return (el.parentNode) ? el.parentNode.removeChild(el) : el; } }; }(); // Now you can do: DOM.getFirst("parent") // first child // or DOM.getFirst("parent", "p") // first p tag child // or var el = DOM.get("parent") // get element by id DOM.getFirst(el) // first child </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload