JQuery Programmer Question: Download JQuery Programmer PDF

Create a plugin that would add and remove a class on hover?

Tweet Share WhatsApp

Answer:

The plugin can be considered to be simply a new method that can be used by a user to extend the prototype object of a jquery. A plugin performs some actions on a collection of elements. Each method that comes with the jquery core can be considered to be a plugin.

The code for creating a plugin that would add and remove a class on hover would be as follows:
(function($)
{
$.fn.hoverClass = function(c)
{
return this.hover(
function() { $(this).toggleClass(c); }
);
};
})(jQuery);

// using the plugin
$('li').hoverClass('hover');

Download JQuery Programmer PDF Read All 201 JQuery Programmer Questions
Previous QuestionNext Question
Explain the common methods of sending a request to a server?Explain what is jQuery?