a simple click event doesn't work
I am building my own plugin, but I'm kind of new to this..
At the beginning, I immediately ran into this problem: a simple click event doesn't get fired.
Calling the plugin works, like so:
- $('img').myPlugin();
The (very basic) structure of my plugin is like the following piece of code
- (function($) {
- $.fn.myPlugin = function() {
- // call function
- return this.each(function() {
- // store element in variable
- var self = $(this);
- // bind the click function
- self.bind('click', function() {
- alertAltText(self);
- });
- });
- // alert alternative text
- function alertAltText(obj) {
- alert(obj.attr('alt'));
- }
- }
- })(jQuery);
So, what I want:
If you would click on an image, javascript alerts the alternative text of that image. But this doesn't work. It seems like this really is a noob-problem, but I can't get it to work no matter what I try (but as I said, I'm kind of new to this).
Does anybody know the solution?