1

The following $(this) does not seem to be returning $('.element'). Any ideas why?

$('.element').popover({
  container: $(this)
})
Jeremy Lynch
  • 6,780
  • 3
  • 52
  • 63

3 Answers3

4

That's because popover is not an event function, but rather a function that applies some properties to an object.

You can however use each to iterate through every element :

$('.element').each(function(i, obj) {
   obj.popover({
      container: obj
   });
});
ahren
  • 16,803
  • 5
  • 50
  • 70
Ely
  • 1,189
  • 9
  • 12
2

As noted by others, the context of this isn't as you perhaps expected. Perhaps try looping over all the elements.

$('.element').each(function() {
    $(this).popover({
        container: $(this)
    });
});

Inside the scope of the each function, $(this) will refer to a specific $('.element') from the list of all the elements with that class.

user1573618
  • 1,696
  • 2
  • 20
  • 38
0

In this particular context, this will refer to whatever happens to be the outer scope. It could be the window object, or some containing function.

Your code is equivalent to this:

var obj = {container: $(this)}; // <-- this is defined in outer scope

$('.element').popover(obj);
Josh
  • 44,706
  • 7
  • 102
  • 124