Tuesday, March 11

JQuery Interview Questions

                     Interview Questions on jQuery. Some of this i have faced in several interviews and some question i have collected and gathered for my viewer.
                     Most of the interviewer test your knowledge on jQuery by asking some basic questions i have provided below.Here i have provided few basic question and answer what a interviewer wants to listen from you.
Let us start from the fundamentals
Q.1:-What do you mean by jQuery?
 Ans:-The best answer for this question should be  jQuery is a lightweight JavaScript library which  emphasizes interaction between JavaScript and HTML.
Q-2:-Can you explain the use of "$ function" in jquery with a suitable example.
Ans:-You can answer like this,
Type of this function is "function". and like that there are a lot of anonymous functions available in jQuery.

Q-3:-Can you explain what is jQuery Selectors and give some example.
Ans:-Your answer should be
  • From your webpage  jQuery Selectors are used to select one or a group of HTML elements.
  • Always it starts with dollar sign and parentheses like $()
  • jQuery also supports all the CSS selectors as well as additional custom selectors.
  • In three way you can select the elements in a web document i.e.
                      1.By tag name    Example: $(div)   
                           This will select all the div elements present in that document.
                      2.By class
                           Example: $(".anyclassname")
                           This will select all the elements having same class name.
                      3.By id
                          Example: $("#anyid")
                          This will select  single element which has an ID of  anyid
Q-4:- Difference between width() and css('width').
 Ans:- In jQuery we can change the width of an element in two ways that is by
 .css('width') and the other way is using .width().
  • The only difference between .css('width') and .width() is the data type of value we specify or return from the both functions.
  • Example
  • In .width() we don't have to add "px" but we have to add in .css('width').
  • If you want to return the width of  "divid" element then .width() will return only ineger value as 100 and .css('width') will return as '100px'.  
Q-5:- In jQuery how many methods are there for fade effects to element  and explain it in briefly.
 Ans:- In jQuery  we have three methods for fade effects  to elements: fadeIn,fadeOut,fadeTo.This will change the opacity of element with different animation.
Q-6:- What do you mean by .siblings() method in jQuery?
Ans:- If we want to fetch siblings of every elements in the set of matched elements then we can use this method.We can fetched elements by an optional selector.
Here selector specify the matched elements
<li> content 1 </li>
<li id="content2"> content 2 </li>
<li class="contentclass"> content 3 </li>
<li class="contentclass"> > content 4 </li>
Let's say i want to find the siblings of the element of id "content2" change the text color to Red,then we have to write like this
If we want to change the text color of the element having class as "contentclass",then we have to write by passing a optional selector like this
Q-7:- In which scenario we will use jQuery?
 Ans:- We can use jQuery when we want to apply some CSS or call functions on events or incase traverse the documents etc.
If you want to make ajax based application,then jquery library is the best approach for development.
Q-8:- What are the advantages of using jQuery?
 Ans:- Thease are some advantages of jQuery
  • Without learning any new syntax we can enhancement javascript.
  • We can reuse the same code in every where that is reusable.
  • The codes are very simple,clear and readable.
  • We can call DOM scripting library calls and complex loops.
Q-9:- How bind() ,live() and delegate() method works?
 Ans:-Here is some difference between these three
  • The bind() method will only attach events to those elements which are added before DOM loaded But delegate() and live() will attach to the elements which are added after DOM loaded also.
  • live() function will not work in chaining.It will work only on an element or a selector But delegate() method supports chaining.
  • Example of a delegate with chaining
Q-10:- Which one is better jQuery or javascript and why?
 Ans:-Defenietly jQuery is better than javascript because for developing ajax based application jQuery libray is the best approach.As you know jaquery is simple ,clear ,readable as well as reusable, so it helps a programmers a lot during development.
Q-11:- Give an example of jQuery.data().
 Ans:-jQuery.data() is used to set or return arbitrary data to or from an element.
jQuery.data(element, key, value)
jQuery.data(span, “itm”, { value1: 10, value2: "someitem" });
Q-12:- What is resize() function in jQuery and How can we use it?
 Ans:-Whenever the browser size is changed the resize() function will call and this event can be only used with $(window).
$(window).resize(function() {
$('#mylabel).text('current window size is ' + $(window).width() + 'x' + $(window).height());
Q-13:- What is animate() function in jQuery and How can we use it?
 Ans:-This function is used to apply custom animation to elements.
$(selector).animate({params}, [duration], [easing], [callback])
<div id="Animate">
Click here to animate
<div id="animatediv" style="width:100px; height:100px;background-color:blue; position: relative; right: 10px;border-width:5px;">
Here is the jQuery code to animate the  div element "animatediv"
$('# Animate').click(function() {
opacity: 0.50,
left: '+=40',
height: 'toggle'
}, 1000, function() {
// do some code here want to execute after the animation
Q-14:- What is slideToggle() effect in jQuery and How to use?
 Ans:-It gives sliding effect to an element.
slideToggle([ duration] [, easing] [, callback])
$("#animatediv").slideToggle("fast", function(){
//do some coding after this animation
Q-15:- Difference between $(this) and 'this' in jQuery?
 Ans:-Both are references the same element but "this" is used in traditional way while "this" is used with $() it becomes a jQuery object on which we can use the jquery functions.
If  "this" keyword is used in $() then to get the text of the element we can use jQuery function text() else we can get the text by using this.innerText.
Following example give you some clarity
Q-16:- What is each() function in jQuery?
 Ans:-This function will called for every matched element.
$(selector).each(function (index, element))
 This will write text for all div element.
Q-17:- Difference between attr and prop?
Ans:-These are some similarities and differences between prop() and attr()
Both are used to get or set the value of specified property of an element. But the difference is prop() will always returns the current value of property while attr() will returns the default value of the property.
Q-18:- What is param() method in jQuery?
Ans:-This is a method which is used to represent an object/array in serialize manner.When we want to make an ajax request we can use these serialize values as query strings of that URL.
    objemp=new emp();
Q-19:- What is the difference between document.ready() and window.onload?
 Ans:- Both are called while page is loaded but these are some diffences
  • window.onload() is a javascript function while document.ready() is a jQuery event.
  •   document.ready() is called after the DOM is loaded without waiting for the contents to get loaded but window.onload() waits for it.
  • document.ready() executes faster than window.onload().
Q-20:- What do you mean by jQuery.holdReady() function?
 Ans:-By using this function we can hold or release the execution of jQuery's ready event.Hence we have to call this method before we run ready event.To hold the ready event,we have to call jQuery.holdReady(true);  and false for release.
Q-21:- How can you read,write and delete cookies in jQuery?
 Ans:-For this we have to use Dough cookie plugin.
  • Create cookie
    $.dough("mycookie", "cookievalue");
  • Read cookie
  • Delete cookie
    $.dough("mycookie", "remove");
Q-22:- What are .empty(), .remove() and .detach()?
  • .empty():-This  method is used to remove all the child elements from matched elements.
  • .remove():-This method is used to remove all the matched elementas well as will remove all the jQuery data associated with the matched element.
  • .detach():-This  method is same as .remove() method but the .detach() method doesn’t remove jQuery data associated with the matched elements.
  • .remove() method  is always faster than .empty() or .detach() method.
Thanks Shibashish Mohanty

No comments:

Post a Comment

Please don't spam, spam comments is not allowed here.

shibashish mohanty