Usage of id attribute

hi guys, could u please tell me, id attribute for what is useful? I didn’t get it.
thanks

Your code so far


<link href="https://fonts.googleapis.com/css?family=Lobster" rel="stylesheet" type="text/css">
<style>
.red-text {
  color: red;
}

h2 {
  font-family: Lobster, monospace;
}

p {
  font-size: 16px;
  font-family: monospace;
}

.thick-green-border {
  border-color: green;
  border-width: 10px;
  border-style: solid;
  border-radius: 50%;
}

.smaller-image {
  width: 100px;
}

.silver-background {
  background-color: silver;
}
</style>

<h2 class="red-text">CatPhotoApp</h2>
<main>
<p class="red-text">Click here to view more <a href="#">cat photos</a>.</p>

<a href="#"><img class="smaller-image thick-green-border" src="https://bit.ly/fcc-relaxing-cat" alt="A cute orange cat lying on its back."></a>

<div class="silver-background">
  <p>Things cats love:</p>
  <ul>
    <li>cat nip</li>
    <li>laser pointers</li>
    <li>lasagna</li>
  </ul>
  <p>Top 3 things cats hate:</p>
  <ol>
    <li>flea treatment</li>
    <li>thunder</li>
    <li>other cats</li>
  </ol>
</div>

<form action="https://freecatphotoapp.com/submit-cat-photo" id="cat-photo-form">
  <label><input type="radio" name="indoor-outdoor" checked> Indoor</label>
  <label><input type="radio" name="indoor-outdoor"> Outdoor</label><br>
  <label><input type="checkbox" name="personality" checked> Loving</label>
  <label><input type="checkbox" name="personality"> Lazy</label>
  <label><input type="checkbox" name="personality"> Energetic</label><br>
  <input type="text" placeholder="cat photo URL" required>
  <button type="submit">Submit</button>
</form>
</main>

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/604.3.5 (KHTML, like Gecko) Version/11.0.1 Safari/604.3.5.

Challenge: Set the id of an Element

Link to the challenge:

Hello there~!

id attributes are used to uniquely identify an element in your HTML. While you can use id attributes to style your elements with CSS, I believe the more common practise is to style with classes (classes can be set to multiple elements, while ids cannot/should not).

The primary use case for the id attribute, IMO, is to be able to select/target that element with JavaScript and manipulate its attributes in the DOM. :slightly_smiling_face:

2 Likes

Hi! The global ID attribute defines a unique identifier, which should not be repeated throughout the document. Its purpose is to identify the element when linking it, whether in scripts or style sheets (with Css). :wave:t2:

1 Like

Identifying an element happens through “id” attribute. Say you’ve a form with 3 input fields collecting name, email and password. For each of them you describe an id - id=“name”, id=“email”, id=“password” respectively. You then access it through “id” ensuring whatever action you’re performing applies only to that field. Usually the actions are either -

  1. CSS styling
  2. targeting sections of a page through references
  3. DOM manipulation

In contrast, take “name” attribute. It helps identify a group of similar fields and sends “name”:“value” pairs to server. In the code you’ve shown, all check boxes have name=“personality” , all radio buttons have name=“indoor-outdoor”. You want to check collectively what the input for the group with name personality is.

However, you use id=“cat-photo-form” once. This is to ensure you can have another form, say, id=“dog-photo-form” but with same fields. This helps distinguish dog-photo-form fields from cat-photo-form ones, as you’ve unique identifiers for enclosed fields though one id attribute.

1 Like

thank u so much for help

thanks for your explanation

1 Like

:slightly_smiling_face: thank u