I have this snippet (below) of jQuery that adds a separate CSS Selector for each of my elements with a class of '.filter-name'
This works and i am happy with what it does.
jQuery('.filter-name').each(function(i){
jQuery(this).addClass('title'+(i+1));
});
My problem is that I have to fire a 'clicked'
class every time each of the elements is clicked, which is done by the code below, it works but it doesn't look very elegant in the code. is there any way of refining this?
jQuery('.title1').click(function(){
jQuery('.title1').toggleClass('clicked');
});
jQuery('.title2').click(function(){
jQuery('.title2').toggleClass('clicked');
});
jQuery('.title3').click(function(){
jQuery('.title3').toggleClass('clicked');
});
jQuery('.title4').click(function(){
jQuery('.title4').toggleClass('clicked');
});
jQuery('.title5').click(function(){
jQuery('.title5').toggleClass('clicked');
});
jQuery('.title6').click(function(){
jQuery('.title6').toggleClass('clicked');
});
I have tried :
jQuery('.title1, .title2, .title3, .title4, .title5, .title6').click(function(){
jQuery('.title1, .title2, .title3, .title4, .title5, .title6').toggleClass('clicked');
});
but this just fires all of them at the same time, which is not what I want the jQuery to do.
p.s using jQuery in noConflict();
hence the jQuery selector.