I'm trying to make a function callable on a jQuery selector.
Here is what I'm doing on my page:
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>first</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="../myplugin.js"></script>
</head>
<body>
<div class="foo"></div>
<script type="text/javascript">
// my custom function
$(".foo").addGadget({ "src":"gadgets/menu.html", "param":{"filter":"office"}},
function (error, response) {
console.log("hello!!!");
});
</script>
</body>
</html>
So I want to be able to call my custom function addGadget()
on $('foo')
. This works all right:
(function (window, jquery, undefined) {
var that = {};
that.addGadget = $.fn.addGadget = function (options, callback) {
console.log("I'd like to access my element here");
console.log(this);
console.log($(this));
};
return window.myPlugin = that;
}(window, $));
So I can call both
myPlugin.addGadget();
$('.foo').addGadget();
Problem is, I cannot access foo
inside my method.
Question:
What do I need to change to be able to access foo
inside the my addGadget
method?
Thanks!