River
River

Reputation: 1487

jquery/Javascript syntax

I am trying to figure out what the below syntax do. It's code from Bootstraps popover.

//title is a string
$tip.find('.popover-title')[ $.type(title) == 'object' ? 'append' : 'html' ](title)
                           ^                                                 ^
                       What does this symbol mean?                    Whats happening here?

Upvotes: 4

Views: 122

Answers (3)

Blender
Blender

Reputation: 298392

Let's break it up:

$tip.find('.popover-title')[ $.type(title) == 'object' ? 'append' : 'html' ](title)

It can be broken down into this:

var found = $tip.find('.popover-title');
found[$.type(title) == 'object' ? 'append' : 'html'](title);

And more:

var found = $tip.find('.popover-title');

if ($.type(title) == 'object') {
  found['append'](title);
} else {
  found['html'](title);
}

A little more:

var found = $tip.find('.popover-title');

if ($.type(title) == 'object') {
  found.append(title);
} else {
  found.html(title);
}

Upvotes: 6

Andreas Louv
Andreas Louv

Reputation: 47119

$("selector")["append"](...) is equal to $("selector").append(...)

Upvotes: 2

Naftali
Naftali

Reputation: 146310

It is basically saying:

if(typeof title == 'object') {
    $tip.find('.popover-title').append(title);
}
else {
    $tip.find('.popover-title').html(title);
}

The [...] notation allows for you dynamically select the function from the $tip.find('.popover-title') object.

Upvotes: 5

Related Questions