[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Using getElementsByName on a DIV ?

R.Wieser

4/25/2016 5:37:00 PM

Hello all,

I need to find an IMG element in a DIV. I have written some code to do
just that, but suddenly thought of using getElementsByName on the DIV node.
Alas, that does not seem to work.

#1: can I use getElementsBy* to find sub-elements of a DIV (or other
grouping element) at all ?

#2: if the answer to #1 is "no", is there another command that will do it ?
A google search did not turn up anything in that regard.

Mind you, I've already written a solution. I'm just wondering if my
multi-line recursive function could be replaced by a simpler, single
command.

Now I think of it, I've got pretty-much the same question in regard to
finding an "ancestor" element: I've written some code which moves up the
DOM tree until it finds the sought for element, but maybe there is a
simpler, single command available for that too.

Regards,
Rudy Wieser



40 Answers

ram

4/25/2016 6:34:00 PM

0

"R.Wieser" <address@not.available> writes:
>I need to find an IMG element in a DIV.

div.getElementsByTagName( "img" ) // untested

>#1: can I use getElementsBy* to find sub-elements of a DIV (or other
>grouping element) at all ?

»getElementsByTagName« belongs to the interfaces »Document«
and »Element«. See www.w3.org/TR/dom/.

R.Wieser

4/25/2016 6:57:00 PM

0

Stefan,

> div.getElementsByTagName( "img" ) // untested

Well, I used the result of "parentNode" (instead of "div"), and that
certainly didn't work for me.

> »getElementsByTagName« belongs to the interfaces
> »Document« and »Element«. See www.w3.org/TR/dom/.

That document is less than six months old. I would like to use something
that works on a bit more than just the very latest of browsers if you don't
mind.

Regards,
Rudy Wieser


-- Origional message:
Stefan Ram <ram@zedat.fu-berlin.de> schreef in berichtnieuws
getElementsByTagName-20160425193302@ram.dialup.fu-berlin.de...
> "R.Wieser" <address@not.available> writes:
> >I need to find an IMG element in a DIV.
>
> div.getElementsByTagName( "img" ) // untested
>
> >#1: can I use getElementsBy* to find sub-elements of a DIV (or other
> >grouping element) at all ?
>
> »getElementsByTagName« belongs to the interfaces »Document«
> and »Element«. See www.w3.org/TR/dom/.
>


Ben Bacarisse

4/25/2016 7:11:00 PM

0

"R.Wieser" <address@not.available> writes:

> Stefan,
>
>> div.getElementsByTagName( "img" ) // untested
>
> Well, I used the result of "parentNode" (instead of "div"), and that
> certainly didn't work for me.
>
>> »getElementsByTagName« belongs to the interfaces
>> »Document« and »Element«. See www.w3.org/TR/dom/.
>
> That document is less than six months old. I would like to use something
> that works on a bit more than just the very latest of browsers if you don't
> mind.

getElementsByTagName is one of the oldest methods. It's specified in
the first version of the DOM (about 1998). If it did not work, it's not
for being the new boy in town.

Can you say more than just that it didn't work for you?

<snip>
--
Ben.

R.Wieser

4/25/2016 7:43:00 PM

0

Ben,

> If it did not work, it's not for being the new boy in town.

Thats what I thought too, but that document made me doubt it ...

> Can you say more than just that it didn't work for you?

Well. The FF Javascript Console told me that "Ancestor.getElementsByName
is not a function" (the "Ancestor" variable contains a "parentNode" result).

When I display the "Ancestor" variables contents in an "alert" box it shows
"object HTMLDivElement", which looks to be correct to me.

Thats about it. Anything else I can check ?

By the way, my apologies. I should not have used (the rather meaningless)
"doesn't work" phrase. At least not without some more explaining. :-
Regards,
Rudy Wieser


-- Origional message:
Ben Bacarisse <ben.usenet@bsb.me.uk> schreef in berichtnieuws
8760v5a4ic.fsf@bsb.me.uk...
> "R.Wieser" <address@not.available> writes:
>
> > Stefan,
> >
> >> div.getElementsByTagName( "img" ) // untested
> >
> > Well, I used the result of "parentNode" (instead of "div"), and that
> > certainly didn't work for me.
> >
> >> »getElementsByTagName« belongs to the interfaces
> >> »Document« and »Element«. See www.w3.org/TR/dom/.
> >
> > That document is less than six months old. I would like to use
something
> > that works on a bit more than just the very latest of browsers if you
don't
> > mind.
>
> getElementsByTagName is one of the oldest methods. It's specified in
> the first version of the DOM (about 1998). If it did not work, it's not
> for being the new boy in town.
>
> Can you say more than just that it didn't work for you?
>
> <snip>
> --
> Ben.


ram

4/25/2016 8:07:00 PM

0

"R.Wieser" <address@not.available> writes:
>Well. The FF Javascript Console told me that "Ancestor.getElementsByName
>is not a function" (the "Ancestor" variable contains a "parentNode" result).

I was talking about »getElementsByTagName« not
»getElementsByName« since an img element is determined by
its element /type/ »img« and not by a name attribute.

(»Tag name« is a misnomer for »type name«.)

Stefan Weiss

4/25/2016 9:41:00 PM

0

R.Wieser wrote:
> Now I think of it, I've got pretty-much the same question in regard to
> finding an "ancestor" element: I've written some code which moves up the
> DOM tree until it finds the sought for element, but maybe there is a
> simpler, single command available for that too.

Since this part of your question hasn't been addressed yet: a manual loop,
moving up the tree and checking each ancestor separately, is the best
solution in your case. There is a new(ish) and very convenient method that
can directly find the closest ancestor matching a given query selector, but
it's not well supported yet:

https://developer.mozilla.org/en-US/docs/Web/API/Eleme...
https://dom.spec.whatwg.org/#dom-eleme...


- stefan

Ben Bacarisse

4/25/2016 10:08:00 PM

0

"R.Wieser" <address@not.available> writes:

>> If it did not work, it's not for being the new boy in town.
>
> Thats what I thought too, but that document made me doubt it ...
>
>> Can you say more than just that it didn't work for you?
>
> Well. The FF Javascript Console told me that "Ancestor.getElementsByName
> is not a function" (the "Ancestor" variable contains a "parentNode"
> result).

Stefan has explained the basic issue (using getElementsByName instead of
getElementsByTagName) but you might be curious at to why you get this
error. getElementsByName is not a method of elements but only of
documents.

<snip>
--
Ben.

R.Wieser

4/26/2016 6:57:00 AM

0

Stefan,

> I was talking about »getElementsByTagName« not
> »getElementsByName« since an img element is determined by
> its element /type/ »img« and not by a name attribute.

I was assuming that the "getElementsBy*" commands where a family-group (all
working the same), allowing us to find an elements in different ways.

The responses (yours, ben's) seem to indicate otherwise ...

Regards
Rudy Wieser


-- Origional message:
Stefan Ram <ram@zedat.fu-berlin.de> schreef in berichtnieuws
type-name-20160425210603@ram.dialup.fu-berlin.de...
> "R.Wieser" <address@not.available> writes:
> >Well. The FF Javascript Console told me that
"Ancestor.getElementsByName
> >is not a function" (the "Ancestor" variable contains a "parentNode"
result).
>
> I was talking about »getElementsByTagName« not
> »getElementsByName« since an img element is determined by
> its element /type/ »img« and not by a name attribute.
>
> (»Tag name« is a misnomer for »type name«.)
>


R.Wieser

4/26/2016 7:00:00 AM

0

Stefan,

> There is a new(ish) and very convenient method that can
> directly find the closest ancestor matching a given query
> selector, but it's not well supported yet:

Yeah, while googling I've stumbled over the command too. But exactly its
"new(ish)" character is what made me decide not to use it.

Thanks for mentioning the command though.

Regard,
Rudy Wieser


-- Origional message:
Stefan Weiss <krewecherl@gmail.com> schreef in berichtnieuws
nfm2tc$346$1@news.albasani.net...
> R.Wieser wrote:
> > Now I think of it, I've got pretty-much the same question in regard to
> > finding an "ancestor" element: I've written some code which moves up
the
> > DOM tree until it finds the sought for element, but maybe there is a
> > simpler, single command available for that too.
>
> Since this part of your question hasn't been addressed yet: a manual loop,
> moving up the tree and checking each ancestor separately, is the best
> solution in your case. There is a new(ish) and very convenient method that
> can directly find the closest ancestor matching a given query selector,
but
> it's not well supported yet:
>
> https://developer.mozilla.org/en-US/docs/Web/API/Eleme...
> https://dom.spec.whatwg.org/#dom-eleme...
>
>
> - stefan


R.Wieser

4/26/2016 7:32:00 AM

0

Ben,

> getElementsByName is not a method of elements but
> only of documents.

Yes, thats what I concluded too (but as a novice in this regard wanted to
verify it), and why I asked if there was another command that would indeed
work
for elements.

Hmmm ... It looks that the only "document.getElementsBy*" command that
wishes to work *at all* on the browser I'm using for testing is the
"TagName" one ... Bummer.

Regards,
Rudy Wieser


-- Origional message:
Ben Bacarisse <ben.usenet@bsb.me.uk> schreef in berichtnieuws
87zish8hqk.fsf@bsb.me.uk...
> "R.Wieser" <address@not.available> writes:
>
> >> If it did not work, it's not for being the new boy in town.
> >
> > Thats what I thought too, but that document made me doubt it ...
> >
> >> Can you say more than just that it didn't work for you?
> >
> > Well. The FF Javascript Console told me that
"Ancestor.getElementsByName
> > is not a function" (the "Ancestor" variable contains a "parentNode"
> > result).
>
> Stefan has explained the basic issue (using getElementsByName instead of
> getElementsByTagName) but you might be curious at to why you get this
> error. getElementsByName is not a method of elements but only of
> documents.
>
> <snip>
> --
> Ben.