Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between class and id in jQuery?

Tags:

html

jquery

What is the difference between class and id in jQuery? For example:

<span class="lalal"></span>

and

<span id="lalal"></span>

Because one works good with jQuery and another not really. Thanks.

like image 658
good_evening Avatar asked Nov 29 '22 19:11

good_evening


1 Answers

ID's should be unique on the page, when you have multiple elements with same ID's, jQuery selects only the first one. That's because it doesn't have to bother looking for others as there isn't supposed to be any more – that might explain the weird behaviour you're experiencing.

If you want multiple elements to have the same functionality, give them the same class. If you want to identify a specific element, give it an id. This isn't limited to just jQuery, but to HTML and CSS overall.

like image 106
Tatu Ulmanen Avatar answered Dec 09 '22 18:12

Tatu Ulmanen