Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

This is a code which has 3 buttons (each called #make_post_X) and once clicked on each it will do some stuff and then have the same code - check if what was clicked had active class, and if not it will make it active while making other buttons inactive

$('#make_post_event').live('click', function(){


        $('.new_post #post_type').val('event');
        $('.event_fields').toggle();
         var active = $(this).hasClass('active');
        $(".post_options").removeClass('active');
         if (!active){
                $(this).addClass('active');
         }

    });

    $('#make_post_image').live('click', function(){

        $('.new_post #post_type').val('image');
        $('.event_fields').hide();
          var active = $(this).hasClass('active');
        $(".post_options").removeClass('active');
         if (!active){
                $(this).addClass('active');
         }
    });

    $('#make_post_video').live('click', function(){

        $('.new_post #post_type').val('video');
        $('.event_fields').hide();
      var active = $(this).hasClass('active');
        $(".post_options").removeClass('active');
         if (!active){
                $(this).addClass('active');
         }

    });

How can i put this code

var active = $(this).hasClass('active');
        $(".post_options").removeClass('active');
         if (!active){
                $(this).addClass('active');
         }

in a function which I would be able to call from each live click?

share|improve this question

1 Answer

Explanation:

    //if the following elements don't change in the lifetime of the page
    //better if they get referenced in a scope outside the handler, that way
    //they are not fetched from the DOM every time the handler is fired
var eventFields = $('.event_fields')
  , postType = $('.new_post #post_type')
  , postOptions = $(".post_options")

    //jQuery allows multiple targets by separating them with commas. 
    //This array join method is an easy way to combine them into
    //comma-separated selectors. makes them easy to read as well     
  , selectors = [
      '#make_post_event',
      '#make_post_image',
      '#make_post_video'
    ].join(',')
  ;

//we use the joined selectors here
//if you are on jQuery 1.7, live has been deprecated. use .on()
//if possible, use delegated .on() handlers as well to bind only a single
//handler rather than have each target have it's own handler
$(selectors).on('click', function () {

      //we cache a few stuff that's used multiple times, especially $(this)
  var id = this.id
    , $this = $(this)
      //I noticed your button's only differ at the last segment
      //it's the only difference in the code of each as well
      //so we merge the code, and extract the last segment to know which
    , button = id.substring(id.lastIndexOf('_')+1)
    , active = $this.hasClass('active')
    ;

  //end use that segment accordingly
  postType.val(button);

  //i found a minor difference though, when the button is event, use toggle
  if(button === 'event'){eventFields.toggle();}
  else {eventFields.hide();}

  //other than that, everything else is just about... the same
  postOptions.removeClass('active');
  if (!active) $this.addClass('active');

});
share|improve this answer
1  
Line "$(selector).on('click', function () {" should say "$(selectors)"! – kryger Apr 7 at 10:25

Your Answer

 
discard

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.