Reputation: 111
I am trying to write a jquery code, what it does that on button click, it adds a class to a link "active" to first element, when i presses the button again it will add class to second element and so on. It check the class in prevoious element and add class to next element. I am new so i am stuck
$(".click").click(function(){
$('.pg').each(function() {
if($('.pg').hasClass('selected')){
$('.pg').addClass('selected');
});
});
Upvotes: 0
Views: 52
Reputation: 152206
Checking if previous element has class and adding it to the next one is pointless. In short words - all elements will have this class if the first one has it too. Just try with:
$(".click").click(function(){
$('.pg').toggleClass('selected', $('.pg:first').hasClass('selected'));
});
Upvotes: 0
Reputation: 2344
This script will find the first element that has class 'pg' but not 'selected' and add 'selected' to it
$(".click").click(function() {
$('.pg:not(.selected):eq(0)').addClass('selected')
});
Upvotes: 1