Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #28428
| Path | csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | "danca (Daniele Campagna)" <cyberdanny@tiscalinet.it> |
| Newsgroups | comp.lang.javascript |
| Subject | Recursive function to find a node in the DOM |
| Date | Wed, 15 Jul 2015 09:59:23 +0200 |
| Organization | A noiseless patient Spider |
| Lines | 40 |
| Message-ID | <mo53os$am9$1@dont-email.me> (permalink) |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=windows-1252; format=flowed |
| Content-Transfer-Encoding | 7bit |
| Injection-Date | Wed, 15 Jul 2015 07:57:16 +0000 (UTC) |
| Injection-Info | mx02.eternal-september.org; posting-host="93dc01ef9af982d6d69cb0207cd22879"; logging-data="10953"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX180oay56B9ZwFZTuOV2vAQo" |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.0.1 |
| X-Mozilla-News-Host | news://news.tin.it:119 |
| Cancel-Lock | sha1:mUoxeMsod9RNWKMi9lJ2Qr0EJzI= |
| Xref | csiph.com comp.lang.javascript:28428 |
Show key headers only | View raw
I wrote this small recursive function to find a node (having a name) in
a DOM element:
//usage: nodeFind (element to inspect (object), f.e. a form, name of the
element to search for, 3rd parameter empty at first call)
function nodeFind(el,n,retval){
if (el.name==n) {
return (el)
}else{
if (el.hasChildNodes) {
for (var z=0;z<el.childNodes.length;++z){
var a=el.childNodes.item(z);
retval=nodeFind(a,n,retval)
}
}
}
return retval
//^^^^^^^^^^^^ note!
}
I needed to find a specific control in a form, in order to substitute an
<input readonly> with a <file> (via replaceChild()) to upload a file if
the form is prevalidated, etc.
(then IIRC I discovered that IE was not able to refer to the substitued
control via <form>[name] - this is another story :-) )
I put it in public domain if ever some newbies finds it useful, and
obviously to discuss with "big chops" here if this is robust enough to
be considered cross-browser and definitive.
I am using it from long time and it seems to work well on every browser.
One strange thing is the way recursion is implemented in javascript: I
did not put the "return retval" statement at first, it seems unuseful,
and went crazy before discovering that it is needed.
Dan
--
"Everybody should pay taxes with a smile"
I tried, but they want money.
Back to comp.lang.javascript | Previous | Next — Next in thread | Find similar | Unroll thread
Recursive function to find a node in the DOM "danca (Daniele Campagna)" <cyberdanny@tiscalinet.it> - 2015-07-15 09:59 +0200
Re: Recursive function to find a node in the DOM Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-07-15 11:59 +0200
Re: Recursive function to find a node in the DOM Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-07-15 12:03 +0200
Re: Recursive function to find a node in the DOM "danca (Daniele Campagna)" <cyberdanny@tiscalinet.it> - 2015-07-15 13:36 +0200
Re: Recursive function to find a node in the DOM Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-07-19 12:27 +0200
Re: Recursive function to find a node in the DOM Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-07-19 12:34 +0200
Re: Recursive function to find a node in the DOM "danca (Daniele Campagna)" <cyberdanny@tiscalinet.it> - 2015-07-21 12:08 +0200
Re: Recursive function to find a node in the DOM Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-07-25 14:11 +0200
Re: Recursive function to find a node in the DOM "danca (Daniele Campagna)" <cyberdanny@tiscalinet.it> - 2015-07-25 21:22 +0200
Re: Recursive function to find a node in the DOM Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-07-27 16:12 +0200
Re: Recursive function to find a node in the DOM "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-08-08 04:50 +0200
Re: Recursive function to find a node in the DOM Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-08-08 09:33 +0200
Re: Recursive function to find a node in the DOM "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-08-12 00:48 +0200
Re: Recursive function to find a node in the DOM Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-08-12 01:18 +0200
Re: Recursive function to find a node in the DOM "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-08-13 23:26 +0200
Re: Recursive function to find a node in the DOM Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-08-16 08:48 +0200
Re: Recursive function to find a node in the DOM "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-08-20 02:11 +0200
Re: Recursive function to find a node in the DOM Scott Sauyet <scott.sauyet@gmail.com> - 2015-07-18 15:59 -0700
Re: Recursive function to find a node in the DOM "danca (Daniele Campagna)" <cyberdanny@tiscalinet.it> - 2015-07-21 12:30 +0200
Re: Recursive function to find a node in the DOM Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-07-21 13:53 +0200
Re: Recursive function to find a node in the DOM "danca (Daniele Campagna)" <cyberdanny@tiscalinet.it> - 2015-07-21 14:51 +0200
Re: Recursive function to find a node in the DOM Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-07-21 16:05 +0200
Re: Recursive function to find a node in the DOM "danca (Daniele Campagna)" <cyberdanny@tiscalinet.it> - 2015-07-21 20:35 +0200
Re: Recursive function to find a node in the DOM Scott Sauyet <scott.sauyet@gmail.com> - 2015-07-30 22:10 -0700
Re: Recursive function to find a node in the DOM "danca (Daniele Campagna)" <cyberdanny@tiscalinet.it> - 2015-08-06 10:07 +0200
Re: Recursive function to find a node in the DOM Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-08-06 20:15 +0200
Re: Recursive function to find a node in the DOM "danca (Daniele Campagna)" <cyberdanny@tiscalinet.it> - 2015-08-08 01:21 +0200
Re: Recursive function to find a node in the DOM Scott Sauyet <scott.sauyet@gmail.com> - 2015-07-30 22:09 -0700
Re: Recursive function to find a node in the DOM Denis McMahon <denismfmcmahon@gmail.com> - 2015-07-29 03:04 +0000
Re: Recursive function to find a node in the DOM "danca (Daniele Campagna)" <cyberdanny@tiscalinet.it> - 2015-08-06 10:20 +0200
csiph-web