Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

migrated from Mar 20 '14 at 18:42

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

up vote 10 down vote accepted

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', '')

$('#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.

share|improve this answer
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! – user1502223 Mar 20 '14 at 23:02

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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