Difference between an id and name attribute?

what is the difference between an id and name attribute?

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

<a href="#"><img src="https://www.bit.ly/fcc-relaxing-cat" alt="A cute orange cat lying on its back."></a>

<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>
<form action="https://www.freecatphotoapp.com/submit-cat-photo">
  <label for="indoor"><input id="indoor" type="radio" name="indoor-outdoor"> Indoor</label>
  <label for="outdoor"><input id="outdoor" type="radio" name="indoor-outdoor"> Outdoor</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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36.

Challenge: Create a Set of Checkboxes

Link to the challenge:

An id has higher specificity than a name attribute because it is unique. Elements should not have the same id attribute. Whereas elements can have the same name attribute.

The name attribute is associated with POST data that is posted as a value in a form submission. It is used as a variable reference for the data so it can be programatically used by JavaScript, PHP, etc to manipulate the data or store it in a database.

The id is typically used in CSS and JQuery selectors to get a specific element and perform some action or styling on it. Whereas many elements can belong to the same class (have the same class attribute.)

Sometimes you want to style or change every element that has a certain class. Other times you need more granularity and want to select one very specific element.

id allows us to do this.

Hope that helps!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.