Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Ayman Elash
Full Stack JavaScript Techdegree Student 5,877 Points(dropdown toggling) Is there any better code to do the same? $(document).click(function(e)
$(document).click(function(e){ if(e.target.className == 'icon-search') { return false; } else if (e.target.className == 'search-input-custom') { return false; } else { $('#properity_search').fadeOut('fast'); $('#search_icon').fadeIn('fast'); }
if(e.target.className == 'table') { return false; } else if (e.target.className == 'dropdown-toggle') { return false; } else { $('.table').fadeOut('fast'); $(".big-overlay").removeClass("z-big-overlay"); } })
1 Answer
Tim Strand
22,458 Points$(document).click(function(e){ if(e.target.className == 'icon-search') { return false; } else if (e.target.className == 'search-input-custom') { return false; } else { $('#properity_search').fadeOut('fast'); $('#search_icon').fadeIn('fast'); } if(e.target.className == 'table') { return false; } else if (e.target.className == 'dropdown-toggle') { return false; } else { $('.table').fadeOut('fast'); $(".big-overlay").removeClass("z-big-overlay"); } })
could become. there is really no need to have 2 separate checks to return false. there is also really no need to return false. just do the action if the criteria are met.
$(document).click(function(e) {
const targetClass = e.target.className;
if(targetClass !== 'icon-search' || targetClass !== 'search-input-custom') {
$('#properity_search').fadeOut('fast');
$('#search_icon').fadeIn('fast');
}
if(targetClass !== 'table' || targetClass !== 'dropdown-toggle) {
$('.table').fadeOut('fast');
$(".big-overlay").removeClass("z-big-overlay");
}
});