Safe ads

Friday, 10 February 2017

JQuery Interview Questions Part - 4

JQuery Interview Questions Part - 4


JQuery Interview Questions 


Q 1.     What are Effects?
Ans 1. Effects are the visible changes in the appearance of the selected HTML elements that get triggered by some user action.

                      

For example –
•      Hiding an element on the click of a mouse
$(“button”).click(function()
{
        $(“p”).hide();
});

•      Change in the dimensions of an HTML element when you hover over an element

$(“button”).click(function()
{
        $(“p”).animate({height:’250px’, width:’250px’}, slow);
});

Q 2.     How is hide effect method useful?
Ans 2. “Hide” effect is used to hide a visible HTML element. It takes two parameters – speed(optional)  and callback(optional).

Syntax –
•      $(selector).hide(speed, callback)
Example –
$(“button”).click(function()
{
        $(“p”).hide();
});

Q 3.     How is show effect method useful?
Ans 3. “Show” effect is used to show a hidden HTML element. It takes two parameters – speed(optional)  and callback(optional).

Syntax –
•      $(selector).show(speed, callback)
Example –
$(“button”).click(function()
{
        $(“p”).show();
});

Q 4.     How is toggle effect method useful?
Ans 4. “toggle” effect is used to toggle between hide and show effects. It takes two parameters – speed(optional)  and callback(optional).

Syntax –
•      $(selector).toggle(speed, callback)
Example –
$(“button”).click(function()
{
        $(“p”).toggle();
});

Q 5.     How is fadeIn effect method useful?
Ans 5. “fadeIn” effect is used to fade in a hidden HTML element. It takes two parameters – speed(optional)  and callback(optional).

Syntax –
•      $(selector).fadeIn(speed, callback)
Example –
$(“button”).click(function()
{
        $(“p”).fadeIn();
});

Q 6.     How is fadeOut effect method useful?
Ans 6. “fadeOut” effect is used to fade out a visible HTML element. It takes two parameters – speed(optional) and callback(optional).

Syntax –
•      $(selector).fadeOut(speed, callback)
Example –
$(“button”).click(function()
{
        $(“p”).fadeOut();
});

Q 7.     How is fadeTo effect method useful?
Ans 7. “fadeTo” effect is used to fade an HTML element to a given opacity. The opacity value ranges from 0 to 1. It takes three parameters – speed, opacity and callback.

Syntax –
•      $(selector).fadeTo(speed, opacity, callback)
Example –
$(“button”).click(function()
{
        $(“p”).fadeTo(“slow”,  0.3);
});

Q 8.     How is fadeToggle effect method useful?
Ans 8. “fadeToggle” effect toggles between fadeIn and fadeOut effects. It takes two parameters – speed(optional)  and callback(optional).

Syntax –
•      $(selector).fadeToggle(speed, callback)
Example –
$(“button”).click(function()
{
        $(“p”).fadeToggle();
});

Q 9.     How is slideUp effect method useful?
Ans 9. “slideUp” effect is used to slideUp an HTML element. It takes two parameters – speed(optional)  and callback(optional).

Syntax –
•      $(selector).slideUp(speed, callback)
Example –
$(“button”).click(function()
{
        $(“p”).slideUp();
});

Q 10.     How is slideDown effect method useful?
Ans 10. “slideDown” effect is used to slide down an HTML element. It takes two parameters – speed(optional) and callback(optional).

Syntax –
•      $(selector).slideDown(speed, callback)
Example –
$(“button”).click(function()
{
        $(“p”).slideDown();
});

Q 11.     How is slideToggle effect method useful?
Ans 11. “slideToggle” effect is used to toggle between slide up and slide down effects on an HTML element. It takes two parameters – speed(optional) and callback(optional).

Syntax –
•      $(selector).slideToggle(speed, callback)
Example –
$(“button”).click(function()
{
        $(“p”).slideToggle();
});

Q 12.     How is stop effect method useful?
Ans 12. The JQuery stop effect is used to stop an animation or effect before it is finished. The optional stopAll parameter specifies whether also the animation queue should be cleared or not. The optional goToEnd parameter specifies whether or not to complete the current animation immediately.

Syntax –
•      $(selector).stop(stopAll, gotoEnd)
Example –
$(“button”).click(function()
{
        $(“p”).stop();
});

Q 13.     How is animate effect method useful?
Ans 13. The JQuery animate effect is used to create custom animations. The “{parameters}” parameter defines the CSS properties to be animated.Other two parameters are - speed and callback

Syntax –
•      $(selector).animate({parameters}, speed, callback)
Example –
$(“button”).click(function()
{
        $(“p”).animate({height:’250px’, width:’250px’}, slow);
});

Q 14.     How is callback function useful?
Ans 14. A callback function is executed only after the current effect is finished.

Suppose, I hide an HTML element using hide effect and in its callback I show an alert stating “Element has been hidden”.
This alert will only be shown after the element is successfully hidden.
Example –
$(“button”).click(function()
{
        $(“p”).hide(“slow”, function(){
                alert(“Element has been hidden”);
});
});

Q 15.     What is chaining?
Ans 15. You can chain together multiple JQuery effects within a single statement. Thus, all the effects will execute in sequence.

Example –
$(“button”).click(function()
{
        $(“p”).slideUp().slideDown().css(“background-color”, “yellow”);
});



Friday, 3 February 2017

JQuery Interview Questions part - 3

JQuery Interview Questions Part - 3


JQuery Interview Questions 


Q 1.     What are Events?
Ans 1. User’s actions to which a website can respond to are called events.

For example –
•      Clicking an element
$(“button”).click(function()
{
        Your action here on button click…
});

•      Moving mouse over an element
$(“div”).mouseover(function()
{
        Your action here when mouse moves over div…
});

               

Q 2.     What are the various JQuery event methods available?
Ans 2. Following are the various JQuery event methods available –

       Mouse Events:
1.   click
2.   dblclick
3.   mouseenter
4.   mouseleave

        Keyboard Events:
1.   keypress
2.   keydown
3.   keyup

Form Events:
1.   submit
2.   change
3.   focus
4.   blur

Document/Window Events:
1.   load
2.   resize
3.   scroll
4.   unload

Q 3.     How to use JQuery ready method?
Ans 3. The JQuery ready method is used to perform action after the HTML document has loaded.

The function within the “ready” event method executes only after the document has loaded.

SYNTAX -
$(document).ready(function() {
Your code here will execute only after the HTML document has loaded…
});

Example –
$(document).ready(function() {
        $(“button”).click(function(){
                $(“p”).hide();
});
});

The action of hiding the <p> element on button click will execute only after the entire HTML document has loaded.

Q 4.     How to use JQuery click method?
Ans 4. The JQuery click method is used to perform an action after a selected HTML element is clicked i.e. the function within the “click” event method executes only after the user clicks the selected HTML element

SYNTAX -
$(selector).click(function()
{
Your action here on element click…
});

Example –
$(document).ready(function() {
        $(“button”).click(function(){
                $(“p”).hide();
});
});

When user clicks the button the “<p>” element gets hidden.

Q 5.     How to use JQuery mouseenter method?  
Ans 5. The JQuery mouseenter method is used to perform an action when the mouse pointer enters the selected HTML element i.e. the function within the “mouseenter” event method executes only after the mouse pointer enters the selected HTML element.

SYNTAX –
$(selector).mouseenter(function()
{
Your action here when mouse pointer enters the selected HTML element…
});

Example –
$(document).ready(function() {
        $(“div”).mouseenter(function(){
                alert(“Mouse pointer has entered div element”);
});
});

An alert will be shown when the mouse pointer enters the “<div>” element

Q 6.    How to use JQuery mouseleave method?
Ans 6. The JQuery mouseleave method is used to perform an action when the mouse pointer leaves the selected HTML element i.e. the function within the “mouseleave” event method executes only after the mouse pointer leaves the selected HTML element.

SYNTAX –
$(selector).mouseleave(function()
{
Your action here when mouse pointer leaves the selected HTML element…
});

Example –
$(document).ready(function() {
        $(“div”).mouseleave(function(){
                alert(“Mouse pointer has left div element”);
});
});

An alert will be shown when the mouse pointer leaves the “<div>” element

Q 7.     How to use JQuery hover method?
Ans 7. The JQuery hover method gives a combined effect of both the mouseenter and mouseleave methods. It is used to perform an action when the mouse pointer enters a selected HTML element and then an action when it leaves that element.

SYNTAX –
$(selector).hover(function()
{
Your action here when mouse pointer enters the selected HTML element…
},
function()
{
Your action here when mouse pointer leaves the selected HTML element…
});

Example –
$(document).ready(function() {
        $(“div”).hover(function(){
                alert(“Mouse pointer has entered div element”);
},
function(){
        alert(“Mouse pointer has left div element”);
});
});

The first function is dedicated for mouseenter event and the second for mouseleave event.

Q 8.    How to use JQuery on method?
Ans 8. The “on” event method attaches one or more event handlers to the selected HTML element.

SYNTAX: Attaching one event handler–
$(selector).on(“Event_Name”, function(){action here…});

SYNTAX: Attaching multiple event handlers–
$(selector).on(
        {
  Event_Name1:function()
                {
                        Your action here for Event_Name1…
                },
          Event_Name2:function()
                {
                        Your action here for Event_Name2…
                },
          .
          .
          .
          Event_NameN:function()
                {
                        Your action here for Event_NameN…
                }
        }
);

Example: Attaching one event handler–
 $(document).ready(function() {
        $(“div”).on(“click”, function(){
                alert(“Div has been clicked”);
});
});

The above example attaches a click event handler with the “<div>” element. When the “<div>” element is clicked the above alert will be shown.

Example: Attaching multiple event handlers–
 $(document).ready(function() {
        $(“div”).on(
{
click: function()
{
                        alert(“Div has been clicked”);
},
dblclick: function()
{
                alert(“Div has been double clicked”);
}
});
});

The above example will attach two event handlers one for “click” and another for “dblclick” to “<div>”. When “<div>” element is clicked the first alert is shown and when it is double clicked, second alert is shown.

Q 9.    How to use JQuery focus method?
Ans 9. The JQuery focus method is used to perform an action after the form field is focused.

SYNTAX-
$(selector).focus(function(){
        Your action here after the form field gets focused…
});

Example –
$(document).ready( function(){
$(“input”).focus(function(){
        alert(“The input element of form has been focused”);
});
});

The above alert is shown when the input element of the form element is focused.

Q 10. How to use JQuery blur method?
Ans 10. The JQuery focus method is used to perform an action after the form field loses focus.

SYNTAX-
$(selector).blur(function(){
        Your action here after the form field loses focus…
});

Example –
$(document).ready( function(){
$(“input”).blur(function(){
                alert(“The input element of form has lost focus”);
});

});