Below, I'm using the .each()
method to loop through the links of a navigation bar and use each link's data attribute as an argument for myFunc
:
var $nav = $(".nav"),
$navLinks = $nav.find(".nav-item");
$navItems.each(function(idx, elem) {
var $self = $(this);
$self.on("click", function(e) {
e.preventDefault();
myFunc($(this).data(myAttr));
});
});
My problem is that the above code only returns the last item of my loop. Even when I wrap the $elf.on
code in an immediately-invoked function expression, it still calls the last item of my loop. Can anyone tell me what I'm doing wrong here?