





















































Read more about this book |
(For more resources on jQuery, see here.)
To make things easier to remember and apply, we are going to start off with the definition of what we will be referring to when speaking of the basic template that all the plugins we are going to develop should conform to.
Actually, we have already had a quick look at it earlier in the previous chapter, but there's something more definitely worth saying.
From now on, we will call the following code the default structure for our plugins. This is what we will promptly copy and paste into each file we're going to write a plugin into.
jQuery.fn.PLUGINNAME = function() {
return this.each(function() {
// code
});
}
Needless to say, the this.each loop iterates all of the matching elements. We return the jQuery object (this) to allow chaining. We extend the jQuery.fn object; all of the code will be put inside the function.
Also:
For the moment, remember to always avoid referring to the jQuery object with the dollar sign ($), to prevent possible conflicts with other libraries. We'll get to using aliases very soon.
The directory that the plugin resides in will also contain two other files, by default:
As our first plugin, we might want to create something uncomplicated but somewhat impressive: what about something that, when the cursor is hovering over an element, substitutes the text contained with some words of our choice?
Getting started in creating jQuery plugins in not difficult at all. For example, creating this simple plugin should help us in understanding how things actually work.
jQuery.fn.txtHover = function() {
return this.each(function() {
// code
});
};
jQuery(this).text("Text changed");
<!DOCTYPE html>
<html>
<head>
<script src="jquery.js"></script>
<script src="jquery.txthover.js"></script>
<script>
$(document).ready(function() {
$("p#one").txtHover();
});
</script>
</head>
<body>
<p id="one">Some text.</p>
</body>
</html>
The plugin is working correctly so far. When the page loads, the text is changed to what we had defined into the plugin code.
However, there are a couple of things to pay attention to:
There are, however, some similarities with the html() function, which treats the text it operates on as if it were HTML code. That is, passing any tag to the html() function results in having the element possibly containing other elements after the operation (also, the same applies for getting HTML code from within the element), whereas the this function will just treat the HTML code as plain text and not affect any element hierarchy.
Despite the good realization, our plugin still misses the point. Our goal was to activate the text substitution whenever the mouse pointer hovered over the text to be replaced, whereas our current implementation does it right after the page is loaded.
We put it inside the "document ready" statement, after all!
By adding little pieces of code one at a time, we can easily understand what we are going to do and which is the best layout for our plugin. Activating the plugin whenever the mouse pointer hovers over the selected elements is surely another little step that adds up to reach our final goal.
jQuery.fn.txtHover = function() {
return this.each(function() {
jQuery(this).hover(function() {
jQuery(this).text("Mouse hovered");
});
});
};
Our modified code will now look like the following:
jQuery.fn.txtHover = function() {
return this.each(function() {
var oldTxt = jQuery(this).text();
jQuery(this).hover(function() {
jQuery(this).text("Mouse hover");
}, function() {
jQuery(this).text(oldTxt);
});
});
};
We might be a little more satisfied with this evolution of our plugin, even though it's far from complete.
Its functioning is fairly straightforward: we have made use of a variable (oldTxt) to store the old content, and we then have proceeded to using two anonymous functions (function(){ }), passed as arguments to the hover() function, to handle the mouse hover event (write our string) and the mouse out event (text back to original).
There's still something to do though:
Read the documentation for the html() function.
Create a plugin that, once the mouse pointer hovers over an element, displays the HTML code of its content. The content should then change back to normal once the mouse pointer is moved away from that space.
What happens if the html() and text() functions are used one inside the other, that is, $(sel).text($(sel).html()) or $(sel).html($(sel).text())? Just play around a little bit.