Web Analytics Made Easy -
StatCounter Noob question about id atr - CodingForum

Announcement

Collapse
No announcement yet.

Noob question about id atr

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Noob question about id atr

    Hello i am new to programming , learning HTML5/CSS , currently learning about " id attribute " .
    I am trying to understand its use over say a class , reading examples from w3school , this example came
    in mind Tryit Editor v3.1 .
    Why would i use and id instead of class for a position/color change ? What are the benefits of the id ? Thank you

  • #2
    ID is unique, only one per page: <div id="I_am_an_only_child">

    You can have as many elements with the same class" <div class="I_have_a_brother"></div> <div class="I_have_a_brother"></div> <div class="I_have_a_brother"></div> <div class="I_have_a_brother"></div> <div class="I_have_a_brother"></div>

    Do something to a class and they ALL change.
    Do something to an ID and only ONE thing chages
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

    Comment


    • #3
      Originally posted by sunfighter View Post
      ID is unique, only one per page: <div id="I_am_an_only_child">

      You can have as many elements with the same class" <div class="I_have_a_brother"></div> <div class="I_have_a_brother"></div> <div class="I_have_a_brother"></div> <div class="I_have_a_brother"></div> <div class="I_have_a_brother"></div>

      Do something to a class and they ALL change.
      Do something to an ID and only ONE thing chages
      Thank you,i think i understand now!
      Could you name one real life problem where you would use the ID in your webpage ?

      Comment


      • #4
        I can not think of one case where you should an ID instead of a CLASS absolutely. JavaScript is powerful enough to find the element using either. I can think of using IDs to make things simpler; like evaluating a sign up form for your web page. Life is easier giving each <input> an ID. Now as said above we could find the contents of each INPUT other ways, but an ID makes it faster and less confusing.
        Evolution - The non-random survival of random variants.
        Physics is actually atoms trying to understand themselves.

        Comment

        Working...
        X