And just to throw a spanner in the works, eventually you will get to the end of the tree and there will be no descendants. In this case, e[i].childNodes will not exist. If you get this problem try this: var d = e[i].childNodes ¦¦ new Array(); I'm not sure without testing but that should create an empty array if there is no childNodes property, so the second loop won't run as the length will be 0. Also, this function will only go 1 level deep, is that what you want? e.g.: <div name='dad_object'> <p>Text <a>Link</a></p> </div> In this example it would pick up the <p> but not the <a>. ...also... It is possible (and correct) for more than one object to have the same name. Think radio buttons. So, the getObjectsByName function returns a list. If there is only 1 object it still returns a list but with only 1 element. I realise this is your own code, and you're not silly enough to break it, but for correctness you should take it into account. Try this: var e = document.getElementsByName("dad_object")[0]; See the [0] on the end? This ensures you will always get the first element.
|