Skip to content

Commit a661ef7

Browse files
rmurpheyajpiano
authored andcommitted
Fix example and other style cleanup in Basic Plugin Creation article. Fixes jquery#310. Fixes jquery#311.
1 parent 7e0a0d0 commit a661ef7

File tree

1 file changed

+13
-34
lines changed

1 file changed

+13
-34
lines changed

page/plugins/basic-plugin-creation.md

+13-34
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title : How to Create a Basic Plugin
33
level: intermediate
44
source: http://jqfundamentals.com/legacy
5-
attribution:
5+
attribution:
66
- jQuery Fundamentals
77
---
88
Sometimes you want to make a piece of functionality available throughout your code;
@@ -19,7 +19,7 @@ $("a").css( "color", "red" );
1919

2020
This is some pretty basic jQuery code, but do you know what's happening behind the scenes? Whenever you use the `$` function to select elements, it returns a jQuery object. This object contains all of the methods you've been using (`css()`, `click()`, etc.), and all of the elements that fit your selector. The jQuery object gets these methods from the `$.fn` object. This object contains all of the jQuery object methods, and if we want to write our own methods, it will need to contain those as well.
2121

22-
Additionally the jQuery utility method `$.trim()` is used above to remove any leading or trailing empty space characters from the user input. Utility methods are functions that reside directly on
22+
Additionally the jQuery utility method `$.trim()` is used above to remove any leading or trailing empty space characters from the user input. Utility methods are functions that reside directly on
2323
`$` function itself. You may occasionally want to write a utility method plugin when your extension to the jQuery API does not have to do something to a set of DOM elements you've retrieved.
2424

2525
##Basic Plugin Authoring
@@ -29,7 +29,7 @@ Let's say we want to create a plugin that makes text within a set of retrieved e
2929
```
3030
$.fn.greenify = function() {
3131
this.css( "color", "green" );
32-
}
32+
};
3333
3434
$("a").greenify(); // makes all the links green
3535
```
@@ -60,14 +60,14 @@ The `$` variable is very popular among JavaScript libraries, and if you're using
6060
$.fn.greenify = function() {
6161
this.css( "color", "green" );
6262
return this;
63-
}
63+
};
6464
6565
$.ltrim = function( str ) {
6666
return str.replace(/^\s+/, '');
67-
}
67+
};
6868
$.rtrim = function( str ) {
6969
return str.replace(/\s+$/, '');
70-
}
70+
};
7171
7272
}( jQuery ));
7373
```
@@ -81,7 +81,7 @@ In addition, the primary purpose of an Immediately Invoked Function is to allow
8181
$.fn.greenify = function() {
8282
this.css( "color", shade );
8383
return this;
84-
}
84+
};
8585
8686
}( jQuery ));
8787
```
@@ -110,35 +110,25 @@ It would be much better to have one slot, and use parameters to control what act
110110
(function( $ ) {
111111
112112
$.fn.popup = function( action ) {
113-
114113
if ( action === "open") {
115-
116114
// Open popup code
117-
118115
} if ( action === "close" ) {
119-
120116
// Close popup code
121-
122117
}
123-
124118
};
125-
}( jQuery ));
119+
}( jQuery ));
126120
```
127121

128-
## Using the `each()` Method
129-
Your typical jQuery object will contain references to any number of DOM elements, and that's why jQuery objects are often referred to as collections.
122+
## Using the `each()` Method
123+
Your typical jQuery object will contain references to any number of DOM elements, and that's why jQuery objects are often referred to as collections.
130124
If you want to do any manipulating with specific elements (e.g. getting data an attribute, calculating specific positions) then you need to use `each()` to
131-
loop through the elements.
125+
loop through the elements.
132126

133127
```
134128
$.fn.myNewPlugin = function() {
135-
136129
return this.each(function() {
137-
138130
// do something to each element here
139-
140131
});
141-
142132
};
143133
```
144134

@@ -155,8 +145,7 @@ accept some options.
155145

156146
```
157147
(function ( $ ) {
158-
159-
$.greenify = function( options ) {
148+
$.fn.greenify = function( options ) {
160149
161150
// This is the easiest way to have default options.
162151
var settings = $.extend({
@@ -192,15 +181,10 @@ we've discussed:
192181
```
193182
(function( $ ){
194183
$.fn.showLinkLocation = function() {
195-
196184
return this.filter("a").each(function() {
197-
198185
$( this ).append( " (" + $( this ).attr("href") + ")" );
199-
200186
});
201-
202-
};
203-
187+
};
204188
}( jQuery ));
205189
206190
// Usage example:
@@ -222,16 +206,11 @@ Our plugin can be optimized though:
222206

223207
```
224208
(function( $ ){
225-
226209
$.fn.showLinkLocation = function() {
227-
228210
return this.filter("a").append(function() {
229-
230211
return " (" + this.href + ")";
231-
232212
});
233213
};
234-
235214
}( jQuery ));
236215
```
237216

0 commit comments

Comments
 (0)