Is there a function that returns all the elements of a particular tag within a particular ID. Example: <div ID="abc"> <span></span> <p></p> <span></span> </div> Now I want all the SPAN elements under ID "abc". Also would the SPAN tags need an ID themselves? Thanks!
Well, $(''abc'').childNodes will get you the 3 tags you mention,
but do
you also want possible children of those tags too?
On 7/3/06, Daniel Elmore
<danielelmore-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Is there a function that returns all the elements of a particular tag
within
> a particular ID.
>
> Example:
>
> <div ID="abc">
> <span></span>
> <p></p>
> <span></span>
> </div>
>
> Now I want all the SPAN elements under ID "abc". Also would the
SPAN tags
> need an ID themselves?
>
> Thanks!
>
> _______________________________________________
> Rails-spinoffs mailing list
> Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
>
Just trying to get a handle on any SPANs so I can fill them in via
innerHTML. So how would I loop over those childNotes and only touch the
SPANs?
Thanks!
-----Original Message-----
From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org]
On Behalf Of Andrew
Kaspick
Sent: Monday, July 03, 2006 1:51 AM
To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: Re: [Rails-spinoffs] Get Elements Within ID
Well, $(''abc'').childNodes will get you the 3 tags you mention,
but do
you also want possible children of those tags too?
On 7/3/06, Daniel Elmore
<danielelmore-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Is there a function that returns all the elements of a particular tag
within> a particular ID.
>
> Example:
>
> <div ID="abc">
> <span></span>
> <p></p>
> <span></span>
> </div>
>
> Now I want all the SPAN elements under ID "abc". Also would the
SPAN tags
> need an ID themselves?
>
> Thanks!
>
> _______________________________________________
> Rails-spinoffs mailing list
> Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
>
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
oops, misread your message, didn''t see that you only wanted a certain
tag type.
Use $(''abc'').getElementsByTagName(''span'')
instead
On 7/3/06, Daniel Elmore
<danielelmore-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Just trying to get a handle on any SPANs so I can fill them in via
> innerHTML. So how would I loop over those childNotes and only touch the
> SPANs?
>
> Thanks!
>
>
> -----Original Message-----
> From:
rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
>
[mailto:rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org]
On Behalf Of Andrew
> Kaspick
> Sent: Monday, July 03, 2006 1:51 AM
> To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> Subject: Re: [Rails-spinoffs] Get Elements Within ID
>
> Well, $(''abc'').childNodes will get you the 3 tags you
mention, but do
> you also want possible children of those tags too?
>
> On 7/3/06, Daniel Elmore
<danielelmore-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> > Is there a function that returns all the elements of a particular tag
> within
> > a particular ID.
> >
> > Example:
> >
> > <div ID="abc">
> > <span></span>
> > <p></p>
> > <span></span>
> > </div>
> >
> > Now I want all the SPAN elements under ID "abc". Also would
the SPAN tags
> > need an ID themselves?
> >
> > Thanks!
> >
> > _______________________________________________
> > Rails-spinoffs mailing list
> > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
> >
> _______________________________________________
> Rails-spinoffs mailing list
> Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
>
> _______________________________________________
> Rails-spinoffs mailing list
> Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
>
How about using the $$ selector?
Find all span elements inside <div> with id "abc".
$$(''div#abc span'').each(function(el) {
//do something
});
I would have to think that
$(''abc'').getElementsByTagName(''span'') would
run faster than making use of $$.
$$ would have to evaulate the entire document. Starting at a known
parent should be much much faster.
On 7/3/06, Maninder, Singh
<mandiv-W2hqgAdRMsX2eFz/2MeuCQ@public.gmane.org>
wrote:> How about using the $$ selector?
>
>
> Find all span elements inside <div> with id "abc".
>
> $$(''div#abc span'').each(function(el) {
> //do something
> });
> _______________________________________________
> Rails-spinoffs mailing list
> Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
>
:)
To be very frank if you don''t use $() and just use
document.getElementById() that would also be faster :))
-----Original Message-----
From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org]On
Behalf Of Andrew
Kaspick
Sent: Monday, July 03, 2006 12:54 PM
To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: Re: [Rails-spinoffs] Get Elements Within ID
I would have to think that
$(''abc'').getElementsByTagName(''span'') would
run faster than making use of $$.
$$ would have to evaulate the entire document. Starting at a known
parent should be much much faster.
On 7/3/06, Maninder, Singh
<mandiv-W2hqgAdRMsX2eFz/2MeuCQ@public.gmane.org>
wrote:> How about using the $$ selector?
>
>
> Find all span elements inside <div> with id "abc".
>
> $$(''div#abc span'').each(function(el) {
> //do something
> });
> _______________________________________________
> Rails-spinoffs mailing list
> Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
>
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
I think $$ could very well be fast enough for what you want to do. Never trade off performance against developer productivity unless you have a very good reason to do it. On 7/3/06, Andrew Kaspick <akaspick-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I would have to think that $(''abc'').getElementsByTagName(''span'') would > run faster than making use of $$. > > $$ would have to evaulate the entire document. Starting at a known > parent should be much much faster. > > > On 7/3/06, Maninder, Singh <mandiv-W2hqgAdRMsX2eFz/2MeuCQ@public.gmane.org> wrote: > > How about using the $$ selector? > > > > > > Find all span elements inside <div> with id "abc". > > > > $$(''div#abc span'').each(function(el) { > > //do something > > }); > > _______________________________________________ > > Rails-spinoffs mailing list > > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs > > > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs >
"Premature optimization is the root of all evil." -Hoare and Knuth :) Am 03.07.2006 um 10:46 schrieb Jon Tirsen:> I think $$ could very well be fast enough for what you want to do. > Never trade off performance against developer productivity unless you > have a very good reason to do it. > > On 7/3/06, Andrew Kaspick <akaspick-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> I would have to think that $(''abc'').getElementsByTagName(''span'') >> would >> run faster than making use of $$. >> >> $$ would have to evaulate the entire document. Starting at a known >> parent should be much much faster. >> >> >> On 7/3/06, Maninder, Singh <mandiv-W2hqgAdRMsX2eFz/2MeuCQ@public.gmane.org> wrote: >> > How about using the $$ selector? >> > >> > >> > Find all span elements inside <div> with id "abc". >> > >> > $$(''div#abc span'').each(function(el) { >> > //do something >> > }); >> > _______________________________________________ >> > Rails-spinoffs mailing list >> > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs >> > >> _______________________________________________ >> Rails-spinoffs mailing list >> Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs >> > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs-- Thomas Fuchs wollzelle http://www.wollzelle.com questentier on AIM madrobby on irc.freenode.net http://www.fluxiom.com :: online digital asset management http://script.aculo.us :: Web 2.0 JavaScript http://mir.aculo.us :: Where no web developer has gone before
Andrew Kaspick wrote:> I would have to think that $(''abc'').getElementsByTagName(''span'') would > run faster than making use of $$. > > $$ would have to evaulate the entire document. Starting at a known > parent should be much much faster.> On 7/3/06, Maninder, Singh >> $$(''div#abc span'').each(function(el) { >> //do something >> });Actually there should not be any noticeable performance difference. The only real difference between: $$(''div#abc span'') and $(''abc'').getElementsByTagName(''span'') is parsing the CSS selector and building a match expression. But the cost of that is fairly low (two regular expression and few statements that are only executed once). Otherwise it will execute the statements above basically the same (find then "abc" element using $() then find all "span" tags that are below that element in the DOM hierarchy by using document.getElementsByTagName). It is informative to take a look at http://dev.rubyonrails.org/browser/spinoffs/prototype/src/selector.js?format=txt Look at the "findElements" method and you can see it does the following: 1. Tries to find the element using $() if an id is given 2. Gets a list of all elements in scope (determined by the previous CSS selector fragment) and filters that list down by tag name if the tag name is given. 3. Once it has filtered the list down as much as possible with standard DOM search methods, only then will it do a linear search of the elements to ensure that the id, class and tag name match the CSS selector. It is quite efficient although there is still room for improvement when finding elements that match a class name. I would only use something other than $$() if I really had some performance constraint. Eric