$30 off During Our Annual Pro Sale. View Details »
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Developing a jQuery Plugin
Search
James Hughes
October 22, 2010
Programming
3
200
Developing a jQuery Plugin
Old presentation from the archives.
James Hughes
October 22, 2010
Tweet
Share
More Decks by James Hughes
See All by James Hughes
Functional Programming with Clojure
kouphax
1
140
Tyrannosaurus Rx
kouphax
0
120
React
kouphax
2
730
Play for (Java|Scala)
kouphax
0
130
Devops: A Case Study
kouphax
0
89
Scala for C# Developers
kouphax
5
2.7k
Dropwizard - Production Ready Web Services
kouphax
3
1.6k
Scala for Fun & Profit
kouphax
4
640
What Agile Means To Me
kouphax
0
150
Other Decks in Programming
See All in Programming
re:Invent 2025 のイケてるサービスを紹介する
maroon1st
0
140
TUIライブラリつくってみた / i-just-make-TUI-library
kazto
1
400
AIコーディングエージェント(Gemini)
kondai24
0
250
実は歴史的なアップデートだと思う AWS Interconnect - multicloud
maroon1st
0
230
Developing static sites with Ruby
okuramasafumi
0
310
ELYZA_Findy AI Engineering Summit登壇資料_AIコーディング時代に「ちゃんと」やること_toB LLMプロダクト開発舞台裏_20251216
elyza
2
400
tsgolintはいかにしてtypescript-goの非公開APIを呼び出しているのか
syumai
7
2.3k
ゆくKotlin くるRust
exoego
1
110
從冷知識到漏洞,你不懂的 Web,駭客懂 - Huli @ WebConf Taiwan 2025
aszx87410
2
2.8k
これならできる!個人開発のすゝめ
tinykitten
PRO
0
120
LT資料
t3tra
6
970
モデル駆動設計をやってみようワークショップ開催報告(Modeling Forum2025) / model driven design workshop report
haru860
0
280
Featured
See All Featured
Statistics for Hackers
jakevdp
799
230k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
37
2.7k
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
63
35k
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
120
What does AI have to do with Human Rights?
axbom
PRO
0
1.9k
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandezseo
0
83
Writing Fast Ruby
sferik
630
62k
Thoughts on Productivity
jonyablonski
73
5k
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
0
840
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
54k
svc-hook: hooking system calls on ARM64 by binary rewriting
retrage
1
16
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
22
Transcript
Introduc)on to jQuery
Developing a Plugin
The Original Func)on /* Make Element Standout */
func)on standout(element){ element.style.border = “1px solid #f00”; } standout(document.getElementById(‘myelement’) );
Steps 1. Convert to Plugin 2. Encapsulate Plugin
Specific Code 3. Support Chaining 4. Expose Global Defaults 5. Support “Per-‐Call” ConfiguraCon 6. Support “Per-‐Element” ConfiguraCon
As a Plugin /* plugin code */
$.fn.standout = func)on() this.css(“border”, “1px solid #f00”); } $(“div”).standout()
Encapsulate Plugin Specific Code (func)on($){
/* plugin code */ $.fn.standout = func)on() debug(this); this.css(“border”, “1px solid #f00”); }; /* private debug method */ func)on debug(o) { console.log(“Count:” + o.length); } })(jQuery);
Facilitate Chaining (func)on($){ /*
plugin code */ $.fn.standout = func)on() debug(this); return this.css(“border”, “1px solid #f00”); } func)on debug(o) { … } })(jQuery); $(“div”).standout().fadeOut()
Expose Global Defaults /* plugin code
*/ $.fn.standout = func)on() if($.fn.standout.defaults.debug){ debug(this); } return this.css( "border", $.fn.standout.defaults.borderStyle ); } /* global defaults*/ $.fn.standout.defaults = { borderStyle : "1px solid #f00” debug: window.console && window.console.log }
Support “Per-‐Call” Configura)on /* plugin code
*/ $.fn.standout = func)on(opts) var o = $.extend({}, $.fn.standout.defaults, opts); return this.css("border", o.borderStyle); }; /* global defaults */ $.fn.standout.defaults = { borderStyle : "1px solid #f00” debug: window.console && window.console.log } $(“div”).standout({debug:false})
Support “Per-‐Element” Configura)on /* plugin code
*/ $.fn.standout = func)on(opts) var o = $.extend({}, $.fn.standout.defaults, opts); return this.each(func)on() { var t = $(this); var eo = $.meta ? $.extend({}, o, t.data()) : o; t.css("border", eo.borderStyle); }); }; <div class=“{borderStyle: ‘1px solid blue’}”></div>
Sta)c Plugins
getParam Method (func)on($){ /* extract
a parameter from URL */ $.getParam = func)on(name) { var param = new RegExp( '[\\?&]' + name + '=([^&#]*)’ ).exec(window.loca)on.href); return (param) ? param[1] : null; }; })(jQuery); // hip://www.mysite.com/?username=jameshu $.getParam(“username”); // <-‐-‐ “jameshu”
getParam Method (Alterna)ve) $.extend({
/* extract a parameter from URL */ getParam : func)on(name) { var param = new RegExp( '[\\?&]' + name + '=([^&#]*)’ ).exec(window.loca)on.href); return (param) ? param[1] : null; } });
Pseudo Selectors
Modified Input Field Selector /* Add Modified Input Field
Selector*/ $.extend($.expr[':'], { modified: func)on(el, idx, tokens) { /* tokens[3] = args*/ return $(el).is("input") && el.defaultValue != el.value; } }); /* Clear all modified input elements */ $("input:modified") .val(“”);
Ques)ons?