Growing jQuery

12
GROWING JQUERY How to extend jQuery for your needs

description

How to create plugins and extend jQuery.

Transcript of Growing jQuery

Page 1: Growing jQuery

GROWING JQUERY

How to extend jQuery for your needs

Page 2: Growing jQuery

jQuery!

Plugins World

Domination

Page 3: Growing jQuery

Attack of the Libraries!

Page 4: Growing jQuery

Mad Scientist

Page 5: Growing jQuery

Separating the Process

$(‚#id‛,‚div.class‛) $(…).show()

Selection Action

Selection Action jQuery

Page 6: Growing jQuery

Cut & Paste

Page 7: Growing jQuery

Plugins!

Page 8: Growing jQuery

Easy as baking cookies!

$("input:checkbox").click(function(){

var boxes = $(‚input:checkbox‛);

if(boxes.filter(‚:checked‛)

.length < 3) {

this.checked = true;

}

});

jQuery.fn.limit = function(n) {

var self = this;

return this.click(function(){

return self

.filter(":checked").length<=n;

});

};

$("input:checkbox").limit(3);

Boring Method Way Cooler Method

“How can I limit the user to only select up to 3 checkboxes?”

Page 9: Growing jQuery

Cute Plugins

$.fn.vals = function(){

var v=[];

this.each(function(){ v.push( $(this).val() ); });

return v;

});

$.fn.zebra = function(){

return this.filter(‚:odd‛)

.addClass(‚odd‛).end();

};

Page 10: Growing jQuery

Extending jQuery

jQuery.expr[‘:’].mod = ‚i%(m[3]-0)==0‛;

jQuery.expr[‘:’].date = ‚/^\\s*\\d{2}\\/\\d{2}\\/\\d{4}\\s*$/

.test( jQuery(a).text() || ‚‛ )‛;

Page 11: Growing jQuery

Simplicity

$(…).idTabs(2);

$(…).idTabs(‚default‛)

$(…).idTabs(function(id){

alert(‚Clicked ‛+id);

});

$(…).idTabs({

start: ‚default‛,

change: ‚false‛

});

Page 12: Growing jQuery

Questions?

Sean Catchpole [email protected]