Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simplify a if/else jquery snippet

I have a super long jquery snippet that I feel could be simplified with the right logic. I'm afraid to touch it since I finally got it to work.

the "else $target = #none" is literally a "show nothing" statement. I wasn't sure how to express that in a better way.

Thanks so much! -zeem

PS. links are to medical marijuana sites, so NSFW links!

    $(function () {
    var $target = $('#CO1');
    if (mmjsRegion == 'CO') {
        $target = $('#CO1');
    } else {
        $target = $('#none');
    }
    $target.imBannerRotater({
        return_type: 'json',
        data_map: {
            image_name: 'name',
            url_name: 'url'
        },
        image_url: 'http://www.kindreviews.com/wp-content/plugins/geoads/CO1.php',
        base_path: 'http://www.kindreviews.com/wp-content/plugins/geoads/images/',
    });
});

$(function () {
    var $target = $('#CO2');
    if (mmjsRegion == 'CO') {
        $target = $('#CO2');
    } else {
        $target = $('#none');
    }
    $target.imBannerRotater({
        return_type: 'json',
        data_map: {
            image_name: 'name',
            url_name: 'url'
        },
        image_url: 'http://www.kindreviews.com/wp-content/plugins/geoads/CO2.php',
        base_path: 'http://www.kindreviews.com/wp-content/plugins/geoads/images/',
    });
});
$(function () {
    var $target = $('#CO3');
    if (mmjsRegion == 'CO') {
        $target = $('#CO3');
    } else {
        $target = $('#none');
    }
    $target.imBannerRotater({
        return_type: 'json',
        data_map: {
            image_name: 'name',
            url_name: 'url'
        },
        image_url: 'http://www.kindreviews.com/wp-content/plugins/geoads/CO3.php',
        base_path: 'http://www.kindreviews.com/wp-content/plugins/geoads/images/',
    });
});
$(function () {
    var $target = $('#CA1');
    if (mmjsRegion == 'CA') {
        $target = $('#CA1');
    } else {
        $target = $('#none');
    }
    $target.imBannerRotater({
        return_type: 'json',
        data_map: {
            image_name: 'name',
            url_name: 'url'
        },
        image_url: 'http://www.kindreviews.com/wp-content/plugins/geoads/CA1.php',
        base_path: 'http://www.kindreviews.com/wp-content/plugins/geoads/images/',
    });
});
$(function () {
    var $target = $('#CA2');
    if (mmjsRegion == 'CA') {
        $target = $('#CA2');
    } else {
        $target = $('#none');
    }
    $target.imBannerRotater({
        return_type: 'json',
        data_map: {
            image_name: 'name',
            url_name: 'url'
        },
        image_url: 'http://www.kindreviews.com/wp-content/plugins/geoads/CA2.php',
        base_path: 'http://www.kindreviews.com/wp-content/plugins/geoads/images/',
    });
});
$(function () {
    var $target = $('#CA3');
    if (mmjsRegion == 'CA') {
        $target = $('#CA3');
    } else {
        $target = $('#none');
    }
    $target.imBannerRotater({
        return_type: 'json',
        data_map: {
            image_name: 'name',
            url_name: 'url'
        },
        image_url: 'http://www.kindreviews.com/wp-content/plugins/geoads/CA3.php',
        base_path: 'http://www.kindreviews.com/wp-content/plugins/geoads/images/',
    });
});
like image 965
zeemy23 Avatar asked Nov 19 '25 00:11

zeemy23


2 Answers

This should do it:

var r = mmjsRegion,
    s = r == 'CO' ? '#CO1, #CO2, #CO3' : r == 'CA' ? '#CA1, #CA2, #CA3' : '';

$(s).each(function() {
    $(this).imBannerRotater({
        return_type: 'json',
        data_map: {
            image_name: 'name',
            url_name: 'url'
        },
        image_url: 'http://www.kindreviews.com/wp-content/plugins/geoads/' + this.id + '.php',
        base_path: 'http://www.kindreviews.com/wp-content/plugins/geoads/images/',
    });
});
like image 172
Šime Vidas Avatar answered Nov 20 '25 15:11

Šime Vidas


$(["CO;CO1", "CO;CO2", "CO;CO3", "CA;CA1", "CA;CA2", "CA;CA3"]).each(function() {

      var data = this.split(";");
      var id = data[1];
      var region = data[0];

      var $target  = $("#" + id);
      if ( mmjsRegion == region ){
        $target = $("#" + id);
      }
      else{
          $target = $('#none');
        }

      $target.imBannerRotater({
        return_type: 'json',
        data_map: {
          image_name: 'name',
          url_name: 'url'
        },
        image_url: 'http://www.kindreviews.com/wp-content/plugins/geoads/' + id + '.php',
        base_path: 'http://www.kindreviews.com/wp-content/plugins/geoads/images/',
      });

});

I dont see the reason of this..

var $target  = $('#CO1');
      if ( mmjsRegion == 'CO' ){
        $target = $('#CO1');
      }
      else{
          $target = $('#none');
        }
like image 22
Luke Avatar answered Nov 20 '25 16:11

Luke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!