Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML Classes WITH IDs

I'm a little confused about HTML classes and IDs, as I'd like to use them BOTH to describe an HTML element. Is this valid and supported by most browsers?

The motivation for me to use both is this:

  1. I have a CSS style that I would like applied to multiple elements.
  2. I have some AJAX and Javascript that will manipulate those same elements, so I need a way to identify which element is which using an ID.
  3. So I'd like to use an id to identify each element for JS manipulation AND at the same time I would like to specify a class so that the same style is applied from the same css.
like image 439
LeeMobile Avatar asked Dec 09 '22 21:12

LeeMobile


1 Answers

An ID would be unique to only one item, where a class can be used to group many items together. You can use them together as you stated, ID as a unique identifier for Javascript and the class to markup with CSS.

Search for html class vs id to get many articles about this topic.

Example:

<ul>
    <li class="odd" id="item1">First Item in the List</li>
    <li class="even" id="item2">Second Item in the List</li>
    <li class="odd" id="item3">Third Item in the List</li>
</ul>
like image 124
Chris Bartow Avatar answered Dec 24 '22 05:12

Chris Bartow