Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply jQuery function to multiple elements with same class

I have a web page that has multiple sliders on, all with the class '.viewer'

If I add my jQuery

   $('.viewer').carousel('.viewer #simplePrevious', '.viewer #simpleNext');

This doesnt work, Is this because it doesnt know what slider this applies too? Should it not apply to all .viewer elements on the page?

like image 861
Liam Avatar asked Sep 19 '12 08:09

Liam


People also ask

How to loop through all elements with same class in jQuery?

Answer: Use the jQuery each() Method You can simply use the jQuery each() method to loop through elements with the same class and perform some action based on the specific condition.

How do you target multiple elements in jQuery?

To select multiple elements of an html page using multiple elements selector, we pass the element names inside parenthesis, in double quotes, separated by commas. For example: $(“div, p, h2”) this will select all the div, p and h2 elements of a page.

What is $() in jQuery?

In the first formulation listed above, jQuery() — which can also be written as $() — searches through the DOM for any elements that match the provided selector and creates a new jQuery object that references these elements: 1. $( "div.

Can we use same class on multiple elements in HTML?

Different Elements Can Share Same Class Different HTML elements can point to the same class name.


1 Answers

You should use the jQuery method each():

$('.viewer').each(function() {
    $(this).carousel('.viewer #simplePrevious', '.viewer #simpleNext');
});

Check the online doc for more information: http://api.jquery.com/each/

Hope this helps mate.

like image 125
Littm Avatar answered Oct 20 '22 14:10

Littm