I have a simple toggle function that I want to repeat 20+ times, is there a more elegant way of writing this without copy and pasting the same function and changing the numbers over and over?
$(document).ready(function(){
$( ".p1" ).click(function() {
$( ".c1" ).toggleClass("open");
$( ".person" ).toggleClass("fadeout");
$(this).removeClass('fadeout');
});
$( ".p2" ).click(function() {
$( ".c2" ).toggleClass("open");
$( ".person" ).toggleClass("fadeout");
$(this).removeClass('fadeout');
});
$( ".p3" ).click(function() {
$( ".c3" ).toggleClass("open");
$( ".person" ).toggleClass("fadeout");
$(this).removeClass('fadeout');
});
});
HTML for more context:
<div class="group">
<div class="p1"></div>
<div class="p2"></div>
<div class="p3"></div>
<div class="c1"></div>
<div class="c2"></div>
<div class="c3"></div>
</div>
<div class="group">
<div class="p4"></div>
<div class="p5"></div>
<div class="p6"></div>
<div class="c4"></div>
<div class="c5"></div>
<div class="c6"></div>
</div>
This is for a profile page, when you click p1, then c1 opens, p2 then c2 opens, etc. I'm controlling the transitions with CSS
The solutions below worked but I've hit another issue. When p1, p2, etc is open all the other p's need to fadeout. Toggle seems to be creating some inconsistencies.