Quote:
Originally Posted by jmrker
In the examples, do the references to "tagName" and "className":
1. mean to use those labels as is (ie; "tagName" and "className")
OR
2. mean to use the scripts' assigned value in the element, like <... name="Some_tagName" ...>
|
sorry for the confusion, examples don't adequately explain what's going on and how it works.
Here's what it does:
getNodes looks at all the tag in the document, and creates a selection of those; a sub-set. it returns an array of elements that meet the conditions.
what conditions?
getNodes looks at the properties of each tag/element to determine if it should "be on the list" and get added to the return.
these properties are often the same as attribs.
for example,
Code:
<a id="a1" class="anc" href="http://google.com">Google</a>
has a couple properties:
1. like all elemenent, it has a .tagName property.
the link's .tagName is "A".
2. it also has an href attrib. Most standard attribs have a corresponding DOM property name as well. in the case of href, the property name (element.href) is the same as the attrib ("href"). This mean that to read the href attrib/property of the <A>, we can examine element.href.
caveats:
3. some attribs don't have 1:1 matching property names, and some property names don't have corresponding attribs at all. .tagName has no attrib equivalent to name one. Other common attribs compete with javascript reserved words, like "class". In those case, javascript/DOM provides a closely-named replacement for that property. for class it's elmenent.className. Some attribs have the same dom property,
id for example.
view the above anchor tag's href attrib via:
Code:
alert ( document.getElementById('a1').href );//=="http://google.com"
view the above anchor tag's id via:
Code:
alert ( document.getElementById('a1').id);//=="a1"
view the above anchor tag's class via:
Code:
alert ( document.getElementById('a1').className);//=="anc"
though some of the examples are handy, it's important to be aware of the dom properties to make getNodes() work for your own use.
Once you have the dom binding memorized (or handily-listed), you can start to create expressions for getNodes().
One final consideration is that although i've only mentioned tags, getNodes() looks att all nodes, not just full Elements.
This means that getNodes() can find comments, text nodes, xml PIs, unlike the common css selectors utilities.
at it's most basic, passing one dom property name to getNodes() will return any node that has the property defined.
thus:
Code:
getNodes("id");//get any tag with an id attrib
getNodes("className");//get any tag with a class attrib
getNodes("data");//get any text node (only text nodes have a .data property)
getNodes("nodeType");//since all nodes have a nodeType, this gets any tag, any comment, and all text nodes (everything!)
to illustrate the above examples, imagine that you rounded up every node and subnode in the page or document into a list (array).
then, one-by-one, you went though the list and decided which ones you really wanted, and which ones shouldn't be on the list after all.
to determine if one of the nodes should be on the list you check it for a single property; a "smoking gun" that only the nodes you want will have.
this illustration basucally describes getNodes() using one argument, though getNodes() checks before adding it to the lst of "good" nodes, so there never is a "compete" node list to audit...
but just knowing that something has a property is ofter not enough.
usually, you also need to know the value of that property while comparing it to the value you want.
for example, if i'm looking for a content wrapper (<div id="content">), i need to look at all tags that have an id, and return the one(s) whose id attrib matches the one i specified in the second argument. since getNodes always gives out a list of nodes, i simply add [0] after the call to grab the first/only match:
Code:
getNodes("id", "content")[0] === document.getElementById("content")
i don't think id is that useful of a dom property to search since document.getElementById already does that.
but, here's where getNodes() breaks away from the usual dom methods; it's as easy to collect based on
any property as it is an id.
---
i need sleep for today.
given the interest, i am reviewing features and performance, and will keep this page updated.
more soon.