I have a ton of jQuery onClick
events. onClick
I hide/show different UI elements. I was wondering, how can I tidy the code up and make multiple onClick
events more readable?
$('.info_2').on('click', function() {
$('#nav-wrapper').toggleClass('hidden_nav');
$('#card-wrapper').toggleClass('centre_share');
$('.E_info').toggleClass('display');
$('#info-btn').css('opacity', '0');
$('#nav-wrapper').delay(300).toggleClass('hidden');
$('#nav-wrapper').removeClass('display_nav');
$('#nav-wrapper').removeClass('display');
});
$('.info_back').on('click', function() {
$('#nav-wrapper').removeClass('hidden_nav');
$('#nav-wrapper').addClass('display_nav');
$('#nav-wrapper').addClass('display');
$('#info-btn').css('opacity', '1');
$('#nav-wrapper').removeClass('hidden');
$('.E_info').removeClass('display');
$('.E_info').addClass('hidden');
$('#card-wrapper').removeClass('centre_share');
});
$('#info-btn').on('click', function(){
$('#info-btn').toggleClass('close_btn');
$('.o-card_border').toggleClass('info_display card_active');
$('.start_title').toggleClass('hidden remove_flow')
$('#svg_full').attr('class', 'test');
$('#svg_top').attr('class', 'test');
$('#svg_bot').attr('class', 'test');
$('#svg_bot_bot').attr('class', 'test');
$('#svg_bot_right').attr('class', 'test');
$('.rectangle_style_frame3 display').toggleClass('hidden');
$('.triangle_style').toggleClass('hidden');
$('.bg-info').toggleClass('display');
$('.info_CharactersInvolved').toggleClass('display');
$('.info_themes').toggleClass('display');
$('.E_info').toggleClass('display');
});
The code works fine. I just think it looks really ugly. And the readability of it is painful, especially if you're trying to jump onto the project and learn the codebase.
onClick
events look ugly and when trying to go through my code and improve it I had no idea how I would tidy up multipleonClick
events \$\endgroup\$onClick
events? If yes, I don't see any way to make it more readable: factorization is possible only on a few element/actions, and will have a cost on readability. At the opposite, if this ony an example, while a lot of otheronClick
events exist, then it might be considered. In this case, post the entire code. \$\endgroup\$