I'm not 100% sure about the context of each of the 'this' references, and whether they refer to the 'this' that is implicit in the local function invocation or to the 'this' in the enclosing function, but that is probably related to the explanation.
Either way, there is a slight flaw in your code, which would only manifest itself if one of you assigned 'linkClass' to an element that isn't an 'a'. To fix this, do the check for 'a' inside the loop, as the 'is' method will return true if any of the items in the jquery collection matches the criteria and, when this is done outside the loop, will cause all of the elements to have a click handler bound to them, regardless of whether they are an 'a' or not...
$.fn.jquery_plugin = function(options) {
this.each(function() {
if ($(this).is('a' ) ) {
$(this).bind('click', function(){
options.content = options.content || $(this).attr('href');
return javascript_plugin (options), false;
})
})
}
return this;
};
javascript_plugin = function (options) {
return alert(options.content);
};
Alternatively, simply include the 'a' in the selector used in the invocation of your plugin...
$(function() {
$('a.link_class').jquery_plugin()
})
...and remove the need for the 'a' check in the plugin...
$.fn.jquery_plugin = function(options) {
this.each(function() {
$(this).bind('click', function(){
options.content = options.content || $(this).attr('href');
return javascript_plugin (options), false;
})
}
return this;
};
The big question for me, is whether the last 'this' refers to the DOM element being clicked on, or the element for the current iteration of the 'each' loop. It should be the former.
EDIT:
I now see the problem - the fact that 'options' is only being set to the value of the element's href if it is currently empty. After this, it is left alone, and retains the previously set value. Try creating a local variable instead...
$.fn.jquery_plugin = function(defaultOptions) {
this.each(function() {
$(this).bind('click', function(){
var options = createAnOptionsInstance(); //do what it says
options.content = defaultOptions.content || $(this).attr('href');
return javascript_plugin (options), false;
})
}
return this;
};
Your original code doesn't show any option being passed in to the plugin, so you probably need some extra checks for this.