/    Sign up×
Community /Pin to ProfileBookmark

What’s the difference between document[id] and document.all[id]

What’s the difference between document[id] and document.all[id]

[CODE]<script type=”text/javascript”>
var d,n,x;
d=document;
n=’txt’;
d.txt=’text’;
x=d[n];
d.write(x);
d.write(“<br />”);
x=’null’;
x=d.all[n];
d.write(x);
</script>[/CODE]

I can acess d.txt with x=d[n], but I fail to do so with x=d.all[n].
I suppose that any property in document can be accessed with d.all[prop_name]. What’s wrong?
Can anyone help? I am a newbie in javascript.?
Many thanks!

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisDec 13.2007 — The "document.all" structure is a non-W3C implementation used in Internet Explorer (and I think Opera) and should be avoided.

If you are using a browser other than IE, "d.all" would not exist unless you create it.
Copy linkTweet thisAlerts:
@KorDec 13.2007 — I could tell you which is the [I]resemblance[/I] between document.all[id] and document[id]. Both are [I]wrong[/I] in modern javascript.

Except the form's elements, all the other elements can bear only an [B]id[/B], not a [B]name[/B].

To refer an element upon it's id, the correct syntax is:

document.[B]getElementById([/B][I]id[/I][B])[/B]
Copy linkTweet thisAlerts:
@felgallDec 14.2007 — The difference is that one of those invalid commands is slightly shorter than the other and involves deleting fewer wrong characters when replacing it with the correct command.
Copy linkTweet thisAlerts:
@aaron792authorDec 14.2007 — Thank you all!

I got it!

Actually I saw such codes in some real world webpages.

Thanks for all your replies, I won't use them in my coding.

FORZA JUVE!
×

Success!

Help @aaron792 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...