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

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');

2. Explain the common methods of sending a request to a server?

The two most common methods of sending a request to a server are :

1. GET method : The get method is mostly used for non destructive operations. These operations get data from the server and does not change the data on it. A good example of the application of the search query to a server. In most of the cases GET will send all of the data to be sent in the form of a query string.
2. POST method : The POST method is primarily used for destructive operations. These operations can change the data on a server. A good example is a user saving an entry on a site will get the POST request. These requests are not cached by the browser. A query can be a part of a url but any data that is to be sent is done separately as post data.

3. How to get the height of an element using jQuery?

The height( ) method gets the current computed, pixel, height of the first matched element.

4. How to remove set of matched elements using jQuery?

The remove( expr ) method removes all matched elements from the DOM.

5. What are jQuery Selectors? Give some examples?

1. jQuery Selectors are used to select one or a group of HTML elements from your web page.
2. jQuery support all the CSS selectors as well as many additional custom selectors.
3. jQuery selectors always start with dollar sign and parentheses: $().
4. There are three building blocks to select the elements in a web document.

1) Select elements by tag name
Example : $(div)
It will select all the div elements in the document.

2) Select elements by ID
Example : $("#xyzid")
It will select single element that has an ID of xyzid.

3) Select elements by class
Example : $(".xyzclass")
It will select all the elements having class xyzclass.

6. How to set the style property of an element using jQuery?

The css( name, value ) method sets a single style property to a value on all matched elements.

7. Which is the starting point of code execution in jQuery?

The starting point of jQuery code execution is $(document).ready() function which is executed when DOM is loaded.

8. How to get the style property of an element using jQuery?

The css( name ) method returns a style property on the first matched element.

9. What is the difference between .js and .min.js?

jQuery library comes in 2 different versions Development and Production/Deployment. The deployment version is also known as minified version. So .min.js is basically the minified version of jQuery library file. Both the files are same as far as functionality is concerned. but .min.js is quite small in size so it loads quickly and saves bandwidth

10. How to get the direct parent of an element using jQuery?

The parent( [selector] ) method gets the direct parent of an element. If called on a set of elements, parent returns a set of their unique direct parent elements.

Download Interview PDF