Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #31719

Re: How to reference an updated document object and to have all of its elements and associated values if any as well?

Path csiph.com!weretis.net!feeder4.news.weretis.net!feeder5.news.weretis.net!news.solani.org!.POSTED!not-for-mail
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: How to reference an updated document object and to have all of its elements and associated values if any as well?
Date Mon, 14 Nov 2016 17:26:11 +0100
Organization PointedEars Software (PES)
Lines 81
Message-ID <2029592.ElGaqSPkdT@PointedEars.de> (permalink)
References <62558672-b8d4-495a-8ccf-4bce9399d64d@googlegroups.com> <o0c7sb$lr4$1@news.albasani.net>
Reply-To Thomas 'PointedEars' Lahn <cljs@PointedEars.de>
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 8Bit
X-Trace solani.org 1479140772 1386 eJwFwYkBgDAIA8CVxJIExlGe/UfoHQ6NJSfoWKyfVLFGkk9T6oWvhM8eZcRkW7SmWsYf7wUUVRDU (14 Nov 2016 16:26:12 GMT)
X-Complaints-To abuse@news.solani.org
NNTP-Posting-Date Mon, 14 Nov 2016 16:26:12 +0000 (UTC)
User-Agent KNode/4.14.2
X-NNTP-Posting-Host eJwFwQcRwEAMAzBK2QPO1znzh1ApvbTQUVmRTHpA8935m6EthJMD7KpSTknxrwzdXo1x+wEfQRDa
X-User-ID eJwNwgkRADEIBDBLQMsnh7LgX8LdJHqMrf2a2tX93X0yVDJBwszlr+ZocKIdnLub3T3wQpyELCBJBZGlYfoAaxwWcw==
Cancel-Lock sha1:dhhyelbm7JUOPHrGA0d/ekixr3Y=
Xref csiph.com comp.lang.javascript:31719

Show key headers only | View raw


Martin Honnen wrote:

> On 13.11.2016 19:01, justaguy wrote:
>> Now, how do I reference / retrieve all the elements and associated values
>> if any for this document?
> 
> Well, the
>    document
> object contains all elements and associated values. It is not clear in
> which form you want to "retrieve" the elements or values.
> 
>> document.all was for old IE browser only, hence, irrelevant for other
>> modern browsers.
> 
> document.getElementsByTagName('*') gives you all elements as an
> HTMLCollection , see
> https://dom.spec.whatwg.org/#concept-getelementsbytagname/

This URI does not work here inasfar as that it does not navigate to the 
section in question.  The URI that does it is currently

<https://dom.spec.whatwg.org/#dom-document-getelementsbytagname>

Your URI might have worked before just as mine does now, but that is 
precisely the unreliable behavior that you can expect with a "Living 
Standard", a resource that does not remotely meet the criteria for a 
standard.

WHATWG HTML is not the normative resource; instead, it is 

<https://www.w3.org/TR/2015/REC-dom-20151119/#dom-document-getelementsbytagname>

as referred to by

<https://www.w3.org/TR/2016/REC-html51-20161101/infrastructure.html#common-dom-interfaces> pp.

Although in both cases the return value is specified as an object that 
implements the HTMLCollection interface, this is actually compatibly 
implemented only as an object implementing the NodeList interface.  By 
contrast to an HTMLCollection, using the bracket property accessor syntax 
with non-numeric string parameter, or calling the referred object’s 
namedItem() method with an argument of that form, on the return value, does 
not always yield useful results.

In any case, the problem with this approach is that one would have to 
iterate over a lot of irrelevant elements as not all elements can have 
values.  If the question is understood literally, then “value” means the 
“value” attribute, and not all elements have it.

In order to avoid this, there are several alternatives, including:

- calling document.getElementsByTagName() with a specific element type name 
  as argument, maybe several times, and if necessary, aggregate the results
  in an array;

- calling document.querySelectorAll() as provided by implementing the 
  Selectors API with a CSS selector that includes all the relevant element
  type names;

- calling document.evaluate() as provided by implementing DOM Level 3 XPath
  to retrieve references to the relevant element objects and/or their
  “values” directly (according to MDN, supported by all major browsers 
  except Internet Explorer).

However, the OP appears to have an X–Y problem; they appear to have the 
misconception that they need to retrieve the values of all elements to 
achieve their goal.  That is not so.

For example, one can determine which elements the user has added by 
observing mutations to the document tree:

<https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver>

Also, the original code is invalid HTML, and if that is corrected, it will 
still not work as the function is being referred to, but not called.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

How to reference an updated document object and to have all of its elements and associated values if any as well? justaguy <lichunshen84@gmail.com> - 2016-11-13 10:01 -0800
  Re: How to reference an updated document object and to have all of its elements and associated values if any as well? Martin Honnen <martin.honnen@gmx.de> - 2016-11-14 12:40 +0100
    Re: How to reference an updated document object and to have all of its elements and associated values if any as well? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-11-14 14:16 +0100
    Re: How to reference an updated document object and to have all of its elements and associated values if any as well? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-11-14 17:26 +0100
      Re: How to reference an updated document object and to have all of its elements and associated values if any as well? justaguy <lichunshen84@gmail.com> - 2016-11-14 12:48 -0800
  Re: How to reference an updated document object and to have all of its elements and associated values if any as well? JJ <jj4public@vfemail.net> - 2016-11-14 18:51 +0700

csiph-web