-6

It looks like ids and classes are the exact same thing and have the same functionality, however ids are used just to show the coder/designer that they are only changing one item in those other similar items... Is that true?

I watched this helpful video http://www.youtube.com/watch?v=UZ4s1AvIDPM and that's the only difference I could tell.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
  • 1
    You think we'll be able to explain it any better than the THOUSANDS of references on the web that talk about the similarities and differences? – j08691 May 07 '14 at 20:04
  • I looked and couldn't find. – Slightly Not Average May 07 '14 at 20:07
  • I thought that English words `identity` and `class` are quite simple to understand. Therefore `id` means unique identifier for the element of DOM while `class` designates a group of DOM elements which should share the same behaviour and/or look. – ElmoVanKielmo May 07 '14 at 20:12

1 Answers1

0

It looks like ids and classes are the exact same thing and have the same functionality

Look again.

id specifically identifies an element. Each element can have no more than one id and each id must be unique throughout the DOM.

class describes an element in a more generic way, associating it logically with other elements of the same class. An element can have many classes, and the same class can be used many times throughout the DOM.

ids are used just to show the coder/designer that they are only changing one item in those other similar items... Is that true?

No, that's not true at all. ids uniquely identify (hence "id") elements. They can be used by a developer or a designer to target a specific element for some purpose (altering it, styling it, observing it, and so on).

The fact that the person is selecting an id does guarantee that they are targeting only one element (assuming that element exists, otherwise zero elements) as long as the document is well-formed (doesn't break the rule of not re-using id values, in which case the code is incorrect and behavior is undefined).

Conversely, when selecting based on a class value, any number of elements may be selected. So any developer and/or designer (or any other role) would bear that in mind and not assume that there's only one element.

David
  • 208,112
  • 36
  • 198
  • 279
  • What is a DOM please...? – Slightly Not Average May 07 '14 at 20:05
  • @user2252443: https://developer.mozilla.org/en-US/docs/DOM Essentially it's the HTML document made up of all of the elements therein. – David May 07 '14 at 20:06
  • Thanks David. You rock man. – Slightly Not Average May 07 '14 at 20:15
  • DOM stands for `Document Object Model` which is a tree representation of HTML/XML document. Every element in the document is a node in DOM tree. Every element except for `document root` has a parent node and elements can have children. There are two types of nodes: tag nodes and text nodes. Text nodes can't have children. – ElmoVanKielmo May 07 '14 at 20:17