zeemy23
zeemy23

Reputation: 681

need some jquery if-else statement help

the code below is broken, but I'm not sure how. I've definitely made some big assumptions here as a newbie.

I'm basically trying to create an if else where imBannerRotater functions on #cast if the variable is true and #pram if it is false.

How could I fix this to get that result?

The # are URLs.

Thanks!-zeem

$(document).ready(function(){
                    if (mmjsRegionName == 'CO')
                        {
                $("#cast").imBannerRotater({
                    return_type: 'json',
                    data_map: {
                        image_name: 'name',
                        url_name: 'url'
                    },
                        image_url: '#',
                        base_path: '#',
                });
                    }
                    else
                        {
                $("#pram").imBannerRotater({
                                return_type: 'json',
                                data_map: {
                                    image_name: 'name',
                                    url_name: 'url'
                                },
                                    image_url: '#',
                                    base_path: '#',
                });

            });

Upvotes: 0

Views: 336

Answers (3)

Brad Christie
Brad Christie

Reputation: 101614

$(function(){
  var $target  = $('#cast'); // or whatever you want as a default
  if (mmjsRegionName == 'CO'){
    $target = $('#cast');
  }else{
    $target = $('#param');
  }
  $target.imBannerRotater({
    return_type: 'json',
    data_map: {
      image_name: 'name',
      url_name: 'url'
    },
    image_url: '#',
    base_path: '#',
  });
});

Little refactored, but should get you there.

(Though I'm not sure what you mean by "if the variable is true"--check if (mmjsRegionName){ instead of comparing it to a string maybe?)


EDIT

If it is a true/false case, you may be better off using this:

$(function(){
  $(mmjsRegion?'#cast':'#param').imBannerRotater({ // note the in-line if statement
    return_type: 'json',
    data_map: {
      image_name: 'name',
      url_name: 'url'
    },
    image_url: '#',
    base_path: '#',
  });
});

Upvotes: 4

James Long
James Long

Reputation: 4736

The best way to do it would be to create a variable and change it to either "cast" or "pram"

var myID = "pram";
if (mmjsRegionName == 'CO') {
    myID = "cast";
}
$('#' + myID.imBannerRotation({
    //your script here...

Upvotes: 0

Dave
Dave

Reputation: 2417

If you are testing whether mmjsRegionName is true or false, then your if statement should just be:

if(mmjsRegionName) { ...

Upvotes: 0

Related Questions