I'm struggling with the efficiency of my Javascript and I believe there must be a better way to achieve my goal. To demonstrate my issue, I've created a simplified version of my project on JSFiddle.
The main objective is to display information in the large grey box when a block on the left is clicked. This functionality is working in the JSFiddle example! However, I currently have a separate javascript function for each block. (In reality, my project has over 70 blocks)
Each function hides all previously clicked blocks and removes their "clicked" class. The "clicked" class serves as a flag to determine whether to hide or show the information.
Below is an example of one of the functions. Is this the most effective approach? Keep in mind, I have to go through and hide each info block every time in my actual project.
$('.block1').on('click', function (e) {
if ($(this).hasClass('block1-clicked')) {
$('.block1-info').fadeOut();
}
else {
$('.block2-info').fadeOut();
$('.block2').removeClass('block2-clicked');
$('.block3-info').fadeOut();
$('.block3').removeClass('block3-clicked');
$('.block4-info').fadeOut();
$('.block4').removeClass('block4-clicked');
$('.block1-info').fadeIn();
}
$(this).toggleClass('block1-clicked') ;
});
Thank you for your help!