JQuery Programmer Question:
Create a plugin that would add and remove a class on hover?

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');
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');
Previous Question | Next Question |
Explain the common methods of sending a request to a server? | Explain what is jQuery? |