I'm new to jQuery and trying to learn how to refactor my bloated code to make it nicer and better maintainable.

I have a tabbed navigation which I'm using jQuery to hide and show pages depending on the selector that is clicked.

  $('#search_distributors').on 'click', (e) =>

  $('#international_distributors').on 'click', (e) =>

    $('#all_distributors').on 'click', (e) =>
        #distributor_location_search_filter_form, ').hide()

I'm familiar with using functions and stuff but unsure how to DRY this up. If anyone can shed some light on this and help me learn that would be wonderful.


migrated from stackoverflow.com Mar 20 '14 at 18:42

This question came from our site for professional and enthusiast programmers.


It's hard to give an exact answer without knowing your HTML structure, but if we assume that:

  1. Your tab headers (apparently li-elements with classes like .search_locations and ids like #search_distributors) have some common class, such as .tabheader.
  2. Each of your tabs is wrapped in an element with a common class, such as .tab.
  3. You can add a HTML5 data- attribute on your tab headers to designate the tab whiches header it is.

Assumptions 1. and 2. are necessary so that you can use a common function for all the tabs without writing what is essentially the same code thrice.

Number 3. could be implemented like this:

<li class="tabheader search_locations" id="search_distributors" data-tab-id="search_distributors_tab">Search locations</li>

Then you can reduce your CoffeeScript to

$('.tabheader').on 'click', (e) =>
    tabId = $(e.currentTarget).data('tabId')
    $('#' + tabId).show()

See my jsFiddle.

If you, some strange reason, would not be able to alter your HTML in any way, your code would still be easier to maintain if you wrote a common function for displaying a tab, then called it for each of the click events:

showTab = (e, tab, header) ->
    allTabs = [
    if tab instanceof Array
        tab = tab.join(',')
    allHeaders = [

$('#search_distributors').on 'click', (e) =>
    showTab(e, ['#distributor_location_search_filter_form', '#distributor_location_search_container'], 'li.search_locations')

$('#international_distributors').on 'click', (e) =>
    showTab(e, '#international_distributors_location_container', 'li.international')

$('#all_distributors').on 'click', (e) =>
    showTab(e, '#all_distributors_location_container', 'li.all_distributors')

See my other jsFiddle.

Note that there are also multiple jQuery plugins for tab behavior, such as one in Bootstrap and jQuery UI.

  • 1
    \$\begingroup\$ I cant upvote cause my rep isnt high enough on here yet but thank you for the response... I Did something similar to what you suggested in the first option. Where I grab the id...i knew my code was too bloated thank you for the tips! \$\endgroup\$ – user1502223 Mar 20 '14 at 23:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.