![]() |
|
|
| Subject:
recursive DOM tree traversal function
Category: Computers > Programming Asked by: leftist-ga List Price: $5.00 |
Posted:
20 Oct 2004 09:07 PDT
Expires: 19 Nov 2004 08:07 PST Question ID: 417567 |
What I'd like to see is a recursive function that calls itself to explore every node of a browser's DOM. My idea is to attach a .title to certain input fields to mark them for special processing, and then call a generic recursive routine to process every item on the webpage. Please keep in mind that I prefer a clear and generic example, as I am mainly asking this question to learn from the answer. Javascript example preferred, but I can probably adapt a VBA function to javascript without too much trouble. I'm a VB/ASP developer that is picking up javascript, so bonus points if someone can point out the syntax for a javascript equivalent to the VBA For Each Element In collection structure. |
|
| There is no answer at this time. |
|
| Subject:
Re: recursive DOM tree traversal function
From: jeffemminger-ga on 20 Oct 2004 15:54 PDT |
do you want to attach a title to all document elements, or just form elements?
here's how to do both:
function attachToAll() {
var els = document.getElementsByTagName("*");
for (var x = 0; x < els.length; x++) {
els[x].title = "Some title";
}
}
function attachToFormElements() {
var forms = document.forms;
for (var x = 0; x < forms.length; x++) {
var els = forms[x].elements;
for (var y = 0; y < els.length; y++) {
els[y].title = "Some title";
}
}
}
// uncomment the one you want to run...
//window.onload = attachToAll;
//window.onload = attachToFormElements;
additionally, you can make "attachToFormElements" more specific by
checking for a certain input type to attach to, or a certain css class
etc
the construct for enumerating object properties is as such:
var doc = document;
for (prop in doc) {
alert( prop + " = " + doc[prop] );
} |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
| Search Google Answers for |
| Google Home - Answers FAQ - Terms of Service - Privacy Policy |