/    Sign up×
Community /Pin to ProfileBookmark

What is the difference in using na ID or a NAME to refer to an object?

both seem to work fine.

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@khalidali63Apr 08.2003 — HTMLWise

some elements has attribute name some don't same is with id, most of the elements as of today support id attribute,

Name is mostly assosciated with form elements.

DOM wise

you can access one objects reference using getElementById where as getElementsByName wiill return an array of NodeList objects.

Does this help?

Cheers

Khalid
Copy linkTweet thisAlerts:
@Ice3TauthorApr 08.2003 — I dont know what a NodeList is so I dont understand much about getElementsByName

But if I understand correctly I can give an object a name or an ID and will get the same effect also if I use an ID I can refer to that object using document.getElementById(objectsID).

I dont really see how that is usefull since getElementById is such a long comand name to type.

If I have a type=text input, ID=[b]text1[/b] in a form ID=[b]form1[/b] I could refer to its value as:

document.text1.form1.value //in this case it doesnt matter if its an ID or name

or

document.getElementById('text1').value //in this case it must be an ID

other than this is there a compatibility issue with Netscape?
Copy linkTweet thisAlerts:
@gil_davisApr 08.2003 — The NAME attribute is used for forms, images, anchors and navigation. When you want to do navigation within the page, you can specify # and the desired NAME value in the link to navigate to that spot.

There can be more than one object with the same name. This is helpful in forms where you have an array of checkboxes with the same name but different values. When the form is submitted, the only box submitted is the one checked, but regardless of which one is checked, the same name is passed (e.g.: ?someName=someValue).

An ID must be unique. You cannot have more than one object with the same ID. For backward compatibility, if no NAME is specified, the browser will take the ID and copy it into the NAME attribute for you.

You can specify styles by an ID, but not a NAME.<style>
#someID {color: red}
</style>
As a general rule, use the property as intended and you will fare well. Think of an ID as a social security number or driver's licence - one per customer, but anyone can have the same name as yours.
Copy linkTweet thisAlerts:
@Ice3TauthorApr 08.2003 — thanks
Copy linkTweet thisAlerts:
@JonaApr 08.2003 — Hey, Khalid, can you explain what a NodeMap is? I know it's something like an Array of nodes, right?
×

Success!

Help @Ice3T 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.21,
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,
)...