dom-traversal

jQuery $.closest() (Or whichever comes first)

While working a project tonight, I ended up using one .js resource file for two different pages. One page contains a textarea within a div, and another contains a textarea within a td. Wanting to work with the siblings of this textarea, and other children of its parent, I wondered how you could best implement "closest div or td, whicheve...

How to select a single child element using jQuery?

Using jQuery how do I select a single child element? I've looked at the Tranversing API and know I can select all the immediate children img elements like this: $(this).children('img'); And to select the first child img element I could use a subscript like this: $(this).children('img')[0]; But I guess I'm kind of surprised I can't ...

How to write a simple preorder DOM tree traversal algorithm in jQuery?

I'd like to take the code found here: http://www.jslab.dk/articles/non.recursive.preorder.traversal.part2 // HTML element var root = document.documentElement; recursivePreorder(root); // Recusively find and handle all text nodes function recursivePreorder(node) { // If node is a text node if (node.type == 3) { // Do somethin...

jQuery performance when selecting multiple items

More of a curiosity question...when doing the following: $('.selector1, .selector2').doSomething() Does jQuery fully traverse the DOM twice to get each set of objects matching each selector or is it finding all the elements in one traversal of the DOM? ...

Javascript-HTML - how to iterate through all the forms on a page?

How can I iterate through all forms in a document using javascript? ...

Should I use .find(".foo .bar") or .children(".foo").children(".bar")?

When using jQuery for DOM traversal both of these return the same results (I believe): $("whatever").find(".foo .bar") $("whatever").children(".foo").children(".bar") Which is preferable to use? ...

jQuery parent selectors

Is there a jQuery parent selector that traverses up the DOM until the first match is found? Eg: <tr> <td> <div id="foo">hello!</div> </td> </tr> to find the row from the div I am using: $('#foo').parent().parent(); It feels like I should be able to write something like $('#foo').firstParent('tr'); but I can't find...

jquery parent descendant selector

Why is 1 faster than 2? $('#p1').find('span'); $('#p1 span'); ...

Reference HTML node from another element

I have this script, which I thought was relatively simple. It basically makes a tree-layout of an iframe's contents. When parts of the tree are hovered, their corresponding iframe elements are 'selected'. But it isn't working, for the life of me. Here is some quick half-pseudo code: function traverseTree(c,o){ //c = the tree subset ...