Web Development with jQuery®

(Elliott) #1

Writing a Plugin (^) ❘ 245
The following JavaScript provides a simple, to-the-point demonstration of how to use the jQuery
Plugin API to write custom plugins for jQuery:
$.fn.extend({
select : function()
{
// In a jQuery plugin; 'this' is already a jQuery ready object
// Performing an operation like addClass() works on one
// or more items, depending on the selection.
return this.addClass('movieSelected');
},
unselect : function()
{
return this.removeClass('movieSelected');
}
});
var movies = {
ready : function()
{
$('a#movieSelectAll').click(
function(event)
{
event.preventDefault();
$('ul.movieList li').select();
}
);
$(document).on(
'click.movieList',
'ul.movieList li',
function()
{
if ($(this).hasClass('movieSelected'))
{
$(this).unselect();
}
else
{
$(this).select();
}
}
);
}
};
$(document).ready(
function()
{
http://www.it-ebooks.info

Free download pdf