Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #21181 > unrolled thread
| Started by | Ed Jay <edMbj@aes-intl.com> |
|---|---|
| First post | 2013-09-07 08:45 -0700 |
| Last post | 2013-09-08 16:03 -0700 |
| Articles | 20 on this page of 27 — 9 participants |
Back to article view | Back to comp.lang.javascript
getElementById or forms['form'].elements....? Ed Jay <edMbj@aes-intl.com> - 2013-09-07 08:45 -0700
Re: getElementById or forms['form'].elements....? Martin Honnen <mahotrash@yahoo.de> - 2013-09-07 18:07 +0200
Re: getElementById or forms['form'].elements....? Ed Jay <edMbj@aes-intl.com> - 2013-09-07 10:36 -0700
Re: getElementById or forms['form'].elements....? Ken <usenet-so@abdiassoftware.com> - 2013-09-07 13:58 -0500
Re: getElementById or forms['form'].elements....? Ed Jay <edMbj@aes-intl.com> - 2013-09-07 13:18 -0700
Re: getElementById or forms['form'].elements....? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-09-07 22:37 +0200
Re: getElementById or forms['form'].elements....? Ed Jay <edMbj@aes-intl.com> - 2013-09-07 14:28 -0700
Re: getElementById or forms['form'].elements....? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-09-08 08:59 +0200
Re: getElementById or forms['form'].elements....? Ed Jay <edMbj@aes-intl.com> - 2013-09-08 06:55 -0700
Re: getElementById or forms['form'].elements....? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-09-08 17:39 +0200
Re: getElementById or forms['form'].elements....? Gregor Kofler <usenet@gregorkofler.com> - 2013-09-08 20:22 +0200
Re: getElementById or forms['form'].elements....? Stefan Weiss <krewecherl@gmail.com> - 2013-09-09 04:35 +0200
Re: getElementById or forms['form'].elements....? Ed Jay <edMbj@aes-intl.com> - 2013-09-08 22:05 -0700
Re: getElementById or forms['form'].elements....? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-09-09 09:22 +0200
Re: getElementById or forms['form'].elements....? Stefan Weiss <krewecherl@gmail.com> - 2013-09-09 13:50 +0200
Re: getElementById or forms['form'].elements....? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-09-07 22:49 +0200
Re: getElementById or forms['form'].elements....? Ed Jay <edMbj@aes-intl.com> - 2013-09-07 14:25 -0700
Re: getElementById or forms['form'].elements....? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-09-08 01:44 +0200
Re: getElementById or forms['form'].elements....? John Harris <niam@jghnorth.org.uk.invalid> - 2013-09-08 10:04 +0100
Re: getElementById or forms['form'].elements....? Ed Jay <edMbj@aes-intl.com> - 2013-09-08 06:56 -0700
Re: getElementById or forms['form'].elements....? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-09-08 19:25 +0200
Re: getElementById or forms['form'].elements....? Ed Jay <edMbj@aes-intl.com> - 2013-09-08 12:23 -0700
Re: getElementById or forms['form'].elements....? Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-09-09 19:56 +0100
Re: getElementById or forms['form'].elements....? John Harris <niam@jghnorth.org.uk.invalid> - 2013-09-10 10:26 +0100
Re: getElementById or forms['form'].elements....? Ken <usenet-so@abdiassoftware.com> - 2013-09-07 18:36 -0500
Re: getElementById or forms['form'].elements....? Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-09-08 19:17 +0100
Re: getElementById or forms['form'].elements....? Ed Jay <edMbj@aes-intl.com> - 2013-09-08 16:03 -0700
Page 1 of 2 [1] 2 Next page →
| From | Ed Jay <edMbj@aes-intl.com> |
|---|---|
| Date | 2013-09-07 08:45 -0700 |
| Subject | getElementById or forms['form'].elements....? |
| Message-ID | <k8im29d9ajaespd888g0se3fhhj589u4kg@4ax.com> |
I'm being told to us the identifier:
document.getElementById("foo_box").whatever;
instead of:
document.forms['form'].elements['foo_box'].whatever;
Why?
--
Ed Jay (remove 'M' to respond by email)
[toc] | [next] | [standalone]
| From | Martin Honnen <mahotrash@yahoo.de> |
|---|---|
| Date | 2013-09-07 18:07 +0200 |
| Message-ID | <522b4f24$0$6576$9b4e6d93@newsspool3.arcor-online.net> |
| In reply to | #21181 |
Ed Jay wrote:
> I'm being told to us the identifier:
> document.getElementById("foo_box").whatever;
>
> instead of:
> document.forms['form'].elements['foo_box'].whatever;
>
> Why?
At least show the HTML. Do you want to access an element that has an
"id" attribute with the value "foo_box"? Or a form control that has a
"name" attribute with that value?
[toc] | [prev] | [next] | [standalone]
| From | Ed Jay <edMbj@aes-intl.com> |
|---|---|
| Date | 2013-09-07 10:36 -0700 |
| Message-ID | <tvom295g5oc2clki1m0itakhpa48m1ctqd@4ax.com> |
| In reply to | #21182 |
Martin Honnen wrote:
>Ed Jay wrote:
>> I'm being told to us the identifier:
>> document.getElementById("foo_box").whatever;
>>
>> instead of:
>> document.forms['form'].elements['foo_box'].whatever;
>>
>> Why?
>
>At least show the HTML. Do you want to access an element that has an
>"id" attribute with the value "foo_box"? Or a form control that has a
>"name" attribute with that value?
Sorry. I'm addressing an element with an id = 'foo_box.'
--
Ed Jay (remove 'M' to reply by email)
Win the War Against Breast Cancer.
Knowing the facts could save your life.
http://www.breastthermography.info
[toc] | [prev] | [next] | [standalone]
| From | Ken <usenet-so@abdiassoftware.com> |
|---|---|
| Date | 2013-09-07 13:58 -0500 |
| Message-ID | <l0ft2n$37u$1@speranza.aioe.org> |
| In reply to | #21181 |
On 07.09.2013 10:45, Ed Jay wrote:
> I'm being told to us the identifier:
> document.getElementById("foo_box").whatever;
>
> instead of:
> document.forms['form'].elements['foo_box'].whatever;
>
> Why?
It's faster and cleaner (ie. /mostly/ personal preference).
By using the forms approach the browser need to first traverse the
document tree, then traverse the forms branch, then elements branch. By
using getElementById the browser will internally look-up in an id list
(same as used for the window object) which is faster.
That being said: Unless you have a ton of elements you need to look-up
you won't notice any perfomance gain by switching. But it's probably
easier to come back and read the code later if you simnplify it (again,
personal preference).
You can use:
document.forms['form'].elements['foo_box'].whatever;
document.forms.form.foo_box.whatever;
document.getElementById('form').foo_box.whatever;
document.getElementById('foo_box').whatever;
document.querySelector('#foo_box').whatever; // the "new way"
window.foo_box;
and even directly:
foo_box.whatever;
Personally I use getElementById when an element has an id but I am
switching more and more over to querySelector (which is a bit more
powerful).
--
Ken Fyrstenberg
Abdias Software
http://abdiassoftware.com/
(remove usenet- from address)
[toc] | [prev] | [next] | [standalone]
| From | Ed Jay <edMbj@aes-intl.com> |
|---|---|
| Date | 2013-09-07 13:18 -0700 |
| Message-ID | <jf2n291eokojl4vvnf47butnue3uc5p29r@4ax.com> |
| In reply to | #21184 |
Ken wrote:
>On 07.09.2013 10:45, Ed Jay wrote:
> > I'm being told to us the identifier:
> > document.getElementById("foo_box").whatever;
> >
> > instead of:
> > document.forms['form'].elements['foo_box'].whatever;
> >
> > Why?
>
>It's faster and cleaner (ie. /mostly/ personal preference).
>
>By using the forms approach the browser need to first traverse the
>document tree, then traverse the forms branch, then elements branch. By
>using getElementById the browser will internally look-up in an id list
>(same as used for the window object) which is faster.
>
>That being said: Unless you have a ton of elements you need to look-up
>you won't notice any perfomance gain by switching. But it's probably
>easier to come back and read the code later if you simnplify it (again,
>personal preference).
>
>You can use:
>
> document.forms['form'].elements['foo_box'].whatever;
> document.forms.form.foo_box.whatever;
> document.getElementById('form').foo_box.whatever;
> document.getElementById('foo_box').whatever;
> document.querySelector('#foo_box').whatever; // the "new way"
> window.foo_box;
>
>and even directly:
>
> foo_box.whatever;
>
>Personally I use getElementById when an element has an id but I am
>switching more and more over to querySelector (which is a bit more
>powerful).
Thanks. That's the answer I was hoping for.
--
Ed Jay (remove 'M' to reply by email)
Win the War Against Breast Cancer.
Knowing the facts could save your life.
http://www.breastthermography.info
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2013-09-07 22:37 +0200 |
| Message-ID | <XnsA234E61D74389eejj99@194.109.133.133> |
| In reply to | #21185 |
Ed Jay wrote on 07 sep 2013 in comp.lang.javascript:
>>You can use:
>>
>> document.forms['form'].elements['foo_box'].whatever;
>> document.forms.form.foo_box.whatever;
>> document.getElementById('form').foo_box.whatever;
>> document.getElementById('foo_box').whatever;
>> document.querySelector('#foo_box').whatever; // the "new way"
>> window.foo_box;
>>
>>and even directly:
>>
>> foo_box.whatever;
>>
>>Personally I use getElementById when an element has an id but I am
>>switching more and more over to querySelector (which is a bit more
>>powerful).
>
> Thanks. That's the answer I was hoping for.
I suppose you could hope so, false hopes abound.
The answer is wrong, unless you prescribe users to use IE.
And there is a difference between name and id.
And name could be a collection.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | Ed Jay <edMbj@aes-intl.com> |
|---|---|
| Date | 2013-09-07 14:28 -0700 |
| Message-ID | <se6n291hqaabcvl1fv2o3ce5i4rf7kb6t9@4ax.com> |
| In reply to | #21186 |
Evertjan. wrote:
>Ed Jay wrote on 07 sep 2013 in comp.lang.javascript:
>>>You can use:
>>>
>>> document.forms['form'].elements['foo_box'].whatever;
>>> document.forms.form.foo_box.whatever;
>>> document.getElementById('form').foo_box.whatever;
>>> document.getElementById('foo_box').whatever;
>>> document.querySelector('#foo_box').whatever; // the "new way"
>>> window.foo_box;
>>>
>>>and even directly:
>>>
>>> foo_box.whatever;
>>>
>>>Personally I use getElementById when an element has an id but I am
>>>switching more and more over to querySelector (which is a bit more
>>>powerful).
>>
>> Thanks. That's the answer I was hoping for.
>
>I suppose you could hope so, false hopes abound.
>
>The answer is wrong, unless you prescribe users to use IE.
>
>And there is a difference between name and id.
>
>And name could be a collection.
The answer I was hoping for was that either method is acceptable, but get
ElementById is faster. Is the answer I received incorrect in that regard?
If it's wrong, perhaps you could provide the correct answer...?
--
Ed Jay (remove 'M' to respond by email)
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2013-09-08 08:59 +0200 |
| Message-ID | <XnsA2355B826FA0Aeejj99@194.109.133.133> |
| In reply to | #21189 |
Ed Jay wrote on 07 sep 2013 in comp.lang.javascript:
> Evertjan. wrote:
>
>>Ed Jay wrote on 07 sep 2013 in comp.lang.javascript:
>>>>You can use:
>>>>
>>>> document.forms['form'].elements['foo_box'].whatever;
>>>> document.forms.form.foo_box.whatever;
>>>> document.getElementById('form').foo_box.whatever;
>>>> document.getElementById('foo_box').whatever;
>>>> document.querySelector('#foo_box').whatever; // the "new way"
>>>> window.foo_box;
>>>>
>>>>and even directly:
>>>>
>>>> foo_box.whatever;
>>>>
>>>>Personally I use getElementById when an element has an id but I am
>>>>switching more and more over to querySelector (which is a bit more
>>>>powerful).
>>>
>>> Thanks. That's the answer I was hoping for.
>>
>>I suppose you could hope so, false hopes abound.
>>
>>The answer is wrong, unless you prescribe users to use IE.
>>
>>And there is a difference between name and id.
>>
>>And name could be a collection.
>
> The answer I was hoping for was that either method is acceptable, but
> get ElementById is faster. Is the answer I received incorrect in that
> regard? If it's wrong, perhaps you could provide the correct
> answer...?
I gave you "the correct response": WRONG.
[for a complete answer, read the relevant posting history of this NG.]
It is wrong in so many ways, and has been discussed so many times.
I specified some of them above.
As I wrote: If you don't specify, how are we, and more important how is
the scripting engine, to know if you mean an name or an id, and if the
name is used repeatedly, becomming an collection?
>>>> foo_box.whatever;
is never accceptable, as it does not work on many browsers,
and if it does, results are inconsistent,
and it is subject to the above problems.
Even in IE it was not totally trustworthy.
Examples:
=======================
<input name=a value=b>
<script type='text/javascript'>
alert(a.value);
</script>
=======================
no popup in Chrome29, inferring a scripting error.
returns 'b' [probably only in IE] in IE10.
=======================
<input name='a' value='b'>
<input id='a' value='c'>
<script type='text/javascript'>
alert(a.value);
</script>
=======================
returns 'c' in Chrome29
returns 'undefined' in IE10
probably does even worse in elderly IEs.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | Ed Jay <edMbj@aes-intl.com> |
|---|---|
| Date | 2013-09-08 06:55 -0700 |
| Message-ID | <nuvo29lpuadoqhi8pfkhiuec5avvo1a1mu@4ax.com> |
| In reply to | #21198 |
Evertjan. wrote:
>Ed Jay wrote on 07 sep 2013 in comp.lang.javascript:
>
>> Evertjan. wrote:
>>
>>>Ed Jay wrote on 07 sep 2013 in comp.lang.javascript:
>>>>>You can use:
>>>>>
>>>>> document.forms['form'].elements['foo_box'].whatever;
>>>>> document.forms.form.foo_box.whatever;
>>>>> document.getElementById('form').foo_box.whatever;
>>>>> document.getElementById('foo_box').whatever;
>>>>> document.querySelector('#foo_box').whatever; // the "new way"
>>>>> window.foo_box;
>>>>>
>>>>>and even directly:
>>>>>
>>>>> foo_box.whatever;
>>>>>
>>>>>Personally I use getElementById when an element has an id but I am
>>>>>switching more and more over to querySelector (which is a bit more
>>>>>powerful).
>>>>
>>>> Thanks. That's the answer I was hoping for.
>>>
>>>I suppose you could hope so, false hopes abound.
>>>
>>>The answer is wrong, unless you prescribe users to use IE.
>>>
>>>And there is a difference between name and id.
>>>
>>>And name could be a collection.
>>
>> The answer I was hoping for was that either method is acceptable, but
>> get ElementById is faster. Is the answer I received incorrect in that
>> regard? If it's wrong, perhaps you could provide the correct
>> answer...?
>
>I gave you "the correct response": WRONG.
>
>[for a complete answer, read the relevant posting history of this NG.]
>
>It is wrong in so many ways, and has been discussed so many times.
>
>I specified some of them above.
>
>As I wrote: If you don't specify, how are we, and more important how is
>the scripting engine, to know if you mean an name or an id, and if the
>name is used repeatedly, becomming an collection?
>
>
>>>>> foo_box.whatever;
>
>is never accceptable, as it does not work on many browsers,
>and if it does, results are inconsistent,
>and it is subject to the above problems.
>
>Even in IE it was not totally trustworthy.
>
>
>Examples:
>
>=======================
><input name=a value=b>
><script type='text/javascript'>
> alert(a.value);
></script>
>=======================
>
>no popup in Chrome29, inferring a scripting error.
>returns 'b' [probably only in IE] in IE10.
>
>
>
>=======================
><input name='a' value='b'>
><input id='a' value='c'>
><script type='text/javascript'>
> alert(a.value);
></script>
>=======================
>
>returns 'c' in Chrome29
>returns 'undefined' in IE10
>probably does even worse in elderly IEs.
Thanks for your response. I don't understand why you're making a big deal
out of using the name attribute. My question pertained to getElementById
and would strongly suggest I'm using ID's and not names. I asked which
technique is faster...you haven't answered the question, but you've gone
off on wild tangents that have nothing to do with the question.
Seriously, if the question was about comparing addressing techniques, why
would I care about what errors Chrome throws when Name is used? :-))
For the record...my application is restricted to using the Opera browser,
which we supply to our clients along with the app. All pertinent HTML
elements employ the ID attribute.
Thanks, anyway.
--
Ed Jay (remove 'M' to reply by email)
Win the War Against Breast Cancer.
Knowing the facts could save your life.
http://www.breastthermography.info
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2013-09-08 17:39 +0200 |
| Message-ID | <XnsA235B38D32F3Aeejj99@194.109.133.133> |
| In reply to | #21201 |
Ed Jay wrote on 08 sep 2013 in comp.lang.javascript: >>======================= >><input name='a' value='b'> >><input id='a' value='c'> >><script type='text/javascript'> >> alert(a.value); >></script> >>======================= >> >>returns 'c' in Chrome29 >>returns 'undefined' in IE10 >>probably does even worse in elderly IEs. > > Thanks for your response. I don't understand why you're making a big deal > out of using the name attribute. My question pertained to getElementById > and would strongly suggest I'm using ID's and not names. I asked which > technique is faster...you haven't answered the question, but you've gone > off on wild tangents that have nothing to do with the question. > > Seriously, if the question was about comparing addressing techniques, why > would I care about what errors Chrome throws when Name is used? :-)) > > For the record...my application is restricted to using the Opera browser, > which we supply to our clients along with the app. All pertinent HTML > elements employ the ID attribute. Ed, you seem to think this Usenet NG is a sort of helpdesk, where you have a right to restrict the "answers". If so you are wrong in thinking that. No, Usenet is a one-to-many, and my responses are ment for those many. My example shows thet the response given was totally incorrect crossbrowserwize, and that you personally want to use only Opera does noet mean the responses in this NG should not warn the NG-users that the response given was dangerously incorrect. -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | Gregor Kofler <usenet@gregorkofler.com> |
|---|---|
| Date | 2013-09-08 20:22 +0200 |
| Message-ID | <l0if8g$e9i$1@dont-email.me> |
| In reply to | #21201 |
Am 08.09.2013 15:55, Ed Jay meinte: > Thanks for your response. I don't understand why you're making a big deal > out of using the name attribute. My question pertained to getElementById > and would strongly suggest I'm using ID's and not names. I asked which > technique is faster...you haven't answered the question, but you've gone > off on wild tangents that have nothing to do with the question. No. Your original question was along the line "which one is better". (Which results in a "depends".) > Seriously, if the question was about comparing addressing techniques, why > would I care about what errors Chrome throws when Name is used? :-)) > > For the record...my application is restricted to using the Opera browser, > which we supply to our clients along with the app. All pertinent HTML > elements employ the ID attribute. And you know which one is faster on Opera? Gregor
[toc] | [prev] | [next] | [standalone]
| From | Stefan Weiss <krewecherl@gmail.com> |
|---|---|
| Date | 2013-09-09 04:35 +0200 |
| Message-ID | <l0jc6c$hnt$1@news.albasani.net> |
| In reply to | #21201 |
On 2013-09-08 15:55, Ed Jay wrote: > Seriously, if the question was about comparing addressing techniques, why > would I care about what errors Chrome throws when Name is used? :-)) > > For the record...my application is restricted to using the Opera browser, > which we supply to our clients along with the app. There's no reason to focus on only one browser when there are standards compliant methods to do what you need. This type of thinking is what got us into the mess where a lot of companies were hooked on an obsolete browser (IE6). > I asked which technique is faster... Okay, but I can't think of a situation where the performance of different ways of accessing form elements matters. They're all fast enough, even on mobile devices. If everything has IDs, then sure, use getElementById(). If not, something like this would be my method of choice: var elements = document.forms.myForm.elements; And after that, just use elements.foo ... elements.bar ... etc. One minor nitpick. I keep seeing people use code like this: document.forms["myForm"].elements["firstName"].value This suggests that 'form' and 'elements' are special in some way, but they're not. You're just accessing properties. You could just as well write document["forms"].myForm["elements"].firstName["value"] If all the names are valid identifiers, it's much simpler to write document.forms.myForm.elements.firstName.value The square brackets aren't needed. - stefan
[toc] | [prev] | [next] | [standalone]
| From | Ed Jay <edMbj@aes-intl.com> |
|---|---|
| Date | 2013-09-08 22:05 -0700 |
| Message-ID | <3llq29lqirq5ercvok4dsvle5bfmh50k9q@4ax.com> |
| In reply to | #21216 |
Stefan Weiss wrote: >On 2013-09-08 15:55, Ed Jay wrote: >> Seriously, if the question was about comparing addressing techniques, why >> would I care about what errors Chrome throws when Name is used? :-)) >> >> For the record...my application is restricted to using the Opera browser, >> which we supply to our clients along with the app. > >There's no reason to focus on only one browser when there are standards >compliant methods to do what you need. This type of thinking is what got >us into the mess where a lot of companies were hooked on an obsolete >browser (IE6). True...but, as I said, we provide the browser (customized), so for us, there's no need to be generic. > >> I asked which technique is faster... > >Okay, but I can't think of a situation where the performance of >different ways of accessing form elements matters. They're all fast >enough, even on mobile devices. Just askin'. If it doesn't matter, that's fine. If it matters, I should take the route needed for my task. > If everything has IDs, then sure, use >getElementById(). If not, something like this would be my method of choice: > > var elements = document.forms.myForm.elements; > >And after that, just use > > elements.foo ... elements.bar ... etc. > >One minor nitpick. I keep seeing people use code like this: > > document.forms["myForm"].elements["firstName"].value > >This suggests that 'form' and 'elements' are special in some way, but >they're not. You're just accessing properties. You could just as well write > > document["forms"].myForm["elements"].firstName["value"] > >If all the names are valid identifiers, it's much simpler to write > > document.forms.myForm.elements.firstName.value > >The square brackets aren't needed. > Good info. Thanks. -- Ed Jay (remove 'M' to reply by email) Win the War Against Breast Cancer. Knowing the facts could save your life. http://www.breastthermography.info
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2013-09-09 09:22 +0200 |
| Message-ID | <XnsA2365F6BFDE4Aeejj99@194.109.133.133> |
| In reply to | #21220 |
Ed Jay wrote on 09 sep 2013 in comp.lang.javascript:
>>This suggests that 'form' and 'elements' are special in some way, but
>>they're not. You're just accessing properties. You could just as well
>>write
>>
>> document["forms"].myForm["elements"].firstName["value"]
>>
>>If all the names are valid identifiers, it's much simpler to write
>>
>> document.forms.myForm.elements.firstName.value
>>
>>The square brackets aren't needed.
>>
> Good info. Thanks.
It is bad info, IMHO.
The criterium for valuating code IN THIS NG is not just "does it work" but
more so is the shown code readable, understandable, adaptable, servicable
and educational.
Code cases IN THIS NG are examples for all users. It is in that sense
immaterial if you should think that code is "good" when it works just for
your personal "needs" in only Safari, and that bad but "working" code is
good enough for you.
And the square brackets are NEEDED
when the string or serial number is a not a scripted litteral.
Examples:
document.forms["myForms"+formnumber].elements[arrayNames[n]].value
This complex string is where the value of document forms over GBI() is
at its best, when used in a loop and/or in a function:
for (var formnumber=3; formnumber<17; formnumber++)
for (var n=39; n>17; n--)
document.forms["mF"+formnumber].elements[arrNam[n]].value = ar[n];
or
function setSomeFormElement(formnumber,n) {
document.forms["mF"+formnumber].elements[arrNam[n]].value = ar[n];
};
or
var fN = now.getDate(), m = 17;
alert(document.forms[fN - 1].element[m * 2].value );
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | Stefan Weiss <krewecherl@gmail.com> |
|---|---|
| Date | 2013-09-09 13:50 +0200 |
| Message-ID | <l0kcml$1cr$1@news.albasani.net> |
| In reply to | #21224 |
On 2013-09-09 09:22, Evertjan. wrote: >>>If all the names are valid identifiers, it's much simpler to write >>> >>> document.forms.myForm.elements.firstName.value >>> >>>The square brackets aren't needed. >>> >> Good info. Thanks. > > It is bad info, IMHO. > > The criterium for valuating code IN THIS NG is not just "does it work" but > more so is the shown code readable, understandable, adaptable, servicable > and educational. > > Code cases IN THIS NG are examples for all users. It is in that sense > immaterial if you should think that code is "good" when it works just for > your personal "needs" in only Safari, and that bad but "working" code is > good enough for you. > > And the square brackets are NEEDED > when the string or serial number is a not a scripted litteral. Which is why I wrote "If all the names are valid identifiers". > Examples: [snipped; none of those properties are valid identifiers] There are reasons for using the square bracket notation, but accessing form elements like the ones seen in this thread is not one of them. - stefan
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2013-09-07 22:49 +0200 |
| Message-ID | <1925628.HtqMPqW3ec@PointedEars.de> |
| In reply to | #21185 |
Ed Jay wrote:
> Ken wrote:
>> You can use:
>>
>> document.forms['form'].elements['foo_box'].whatever;
>> document.forms.form.foo_box.whatever;
>> document.getElementById('form').foo_box.whatever;
>> document.getElementById('foo_box').whatever;
>> document.querySelector('#foo_box').whatever; // the "new way"
>> window.foo_box;
>>
>>and even directly:
>>
>> foo_box.whatever;
>>
>>Personally I use getElementById when an element has an id but I am
>>switching more and more over to querySelector (which is a bit more
>>powerful).
>
> Thanks. That's the answer I was hoping for.
The wrong answer. But thanks to your address munging, somebody else will
have to give you the correct one.
--
PointedEars
Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Ed Jay <edMbj@aes-intl.com> |
|---|---|
| Date | 2013-09-07 14:25 -0700 |
| Message-ID | <1d6n299ba9cmtagpesrbgp646td9fhtgsr@4ax.com> |
| In reply to | #21187 |
Thomas 'PointedEars' Lahn wrote:
>Ed Jay wrote:
>
>> Ken wrote:
>>> You can use:
>>>
>>> document.forms['form'].elements['foo_box'].whatever;
>>> document.forms.form.foo_box.whatever;
>>> document.getElementById('form').foo_box.whatever;
>>> document.getElementById('foo_box').whatever;
>>> document.querySelector('#foo_box').whatever; // the "new way"
>>> window.foo_box;
>>>
>>>and even directly:
>>>
>>> foo_box.whatever;
>>>
>>>Personally I use getElementById when an element has an id but I am
>>>switching more and more over to querySelector (which is a bit more
>>>powerful).
>>
>> Thanks. That's the answer I was hoping for.
>
>The wrong answer. But thanks to your address munging, somebody else will
>have to give you the correct one.
>
I appreciate your cooperating, although I have no idea wth you're trying
to say.
--
Ed Jay (remove 'M' to respond by email)
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2013-09-08 01:44 +0200 |
| Message-ID | <6036303.Dn5HOMtUXI@PointedEars.de> |
| In reply to | #21188 |
Ed Jay wrote:
> Thomas 'PointedEars' Lahn wrote:
>> Ed Jay wrote:
>>> Ken wrote:
>>>> You can use:
>>>>
>>>> document.forms['form'].elements['foo_box'].whatever;
>>>> document.forms.form.foo_box.whatever;
>>>> document.getElementById('form').foo_box.whatever;
>>>> document.getElementById('foo_box').whatever;
>>>> document.querySelector('#foo_box').whatever; // the "new way"
>>>> window.foo_box;
>>>>
>>>> and even directly:
>>>>
>>>> foo_box.whatever;
>>>>
>>>> Personally I use getElementById when an element has an id but I am
>>>> switching more and more over to querySelector (which is a bit more
>>>> powerful).
>>>
>>> Thanks. That's the answer I was hoping for.
>>
>> The wrong answer. But thanks to your address munging, somebody else will
>> have to give you the correct one.
>
> I appreciate your cooperating, although I have no idea wth you're trying
> to say.
> […]
> Ed Jay (remove 'M' to respond by email)
<http://www.interhack.net/pubs/munging-harmful/>
HTH
--
PointedEars
Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | John Harris <niam@jghnorth.org.uk.invalid> |
|---|---|
| Date | 2013-09-08 10:04 +0100 |
| Message-ID | <2bfo29tuto68rk4vv8rmhu7g5fh00d14pp@4ax.com> |
| In reply to | #21196 |
On Sun, 08 Sep 2013 01:44:03 +0200, Thomas 'PointedEars' Lahn <PointedEars@web.de> wrote: <snip> ><http://www.interhack.net/pubs/munging-harmful/> The author thinks it only takes 10 minutes to select and delete several hundred spam emails. The author thinks that in 1998 it wasn't a problem downloading 50 100kB spam emails over a slow dial-up link. John
[toc] | [prev] | [next] | [standalone]
| From | Ed Jay <edMbj@aes-intl.com> |
|---|---|
| Date | 2013-09-08 06:56 -0700 |
| Message-ID | <ff0p29p7378m1q3q08a4m97sum613rht6q@4ax.com> |
| In reply to | #21200 |
John Harris wrote: >On Sun, 08 Sep 2013 01:44:03 +0200, Thomas 'PointedEars' Lahn ><PointedEars@web.de> wrote: > > > <snip> >><http://www.interhack.net/pubs/munging-harmful/> > >The author thinks it only takes 10 minutes to select and delete >several hundred spam emails. > >The author thinks that in 1998 it wasn't a problem downloading 50 >100kB spam emails over a slow dial-up link. > The author had nothing intelligent to add, but felt the need to make comment. :-)) -- Ed Jay (remove 'M' to reply by email) Win the War Against Breast Cancer. Knowing the facts could save your life. http://www.breastthermography.info
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.javascript
csiph-web