How can I apply a jQuery function to all elements with the same ID?

The IDs for elements are supposed to be unique in the DOM. Having the same ID for two or more elements is not valid html. To share functionality across elements, assign them a common class, rather than giving them the same ID. If you can’t assign them a common class, the workaround below will allow you to select elements with the same id attribute:

Using the same ID (If not possible to change ID)

jQuery(document).ready(function() {
    jQuery('[id=carousel]').jcarousel();
});

Using a common class (Recommended way)

jQuery(document).ready(function() { 
    jQuery('.carousel').jcarousel();
});

Leave a Comment