Jump to content

ID vs. NAME


mjsulliv

Recommended Posts

While exploring the nuances of CSS and Javascript it seems that CSS always reference the element by ID and Javascript by NAME. Is there anything inherently wrong with always having each element having both attributes and them being the same? Thanks for your thoughts.--- Mike

Link to comment
Share on other sites

JavaScript uses IDs too - at least, using the DOM methods it does. Names are only used for forms now.

Link to comment
Share on other sites

Your assumptions are incorrect. Well-designed CSS will refer to elements by className much more frequently than IDs. On the other hand, CSS cannot refer to an element by name at all. So you will of course see more ID selectors than name selectors. But that is not a reason to burden your CSS with a lot of ID selectors.Very few elements actually support a name property. Most of the ones that do are used in forms, because a browser constructs GET or POST data from input names. But a div or span (for example) cannot, by rule, support a name attribute. This makes the utility of names in JavaScript very limited.For backwards compatibility, browsers do allow JavaScript to get a reference to a named element in a form using dot notation. But that does not mean this is the recommended technique. In most cases, good JavaScript refers to IDs.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...