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?
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.
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.
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.
Different Elements Can Share Same Class Different HTML elements can point to the same class name.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With