Matthew
Matthew

Reputation: 15662

jquery - what does this line of code do?

In the colorbox plugin, I've got this line:

.css({width: getWidth(), overflow: settings.scrolling ? 'auto' : 'hidden'})

For my purposes, I need the overflow set to 'visible'. If I do this:

.css({width: getWidth(), overflow: settings.scrolling ? 'visible' : 'visible'})

then everything works fine. But I'd like to understand what's going on (mostly at the end of the line). I'd guess it means something like "if the setting is this then do this, otherwise do this". Is that right? I guess I just don't understand what the syntax is and such.

Thanks.

Upvotes: 1

Views: 67

Answers (2)

Greg W
Greg W

Reputation: 5239

What you're looking at is a ternary statement. Essentially its a shortcut for an If/Else.

settings.scrolling ? 'auto' : 'hidden';

Is equivalent to

if(settings.scrolling){
  return 'auto';
}
else{
  return 'hidden';
}

Upvotes: 3

Pat
Pat

Reputation: 25685

You can just do this:

.css({width: getWidth(), overflow: 'visible'})

And yes, you're right about what the last line is doing. It's a ternary operation.

Upvotes: 1

Related Questions