jQuery Interview Questions Part 5

21. Why is jQuery library used for client scripting?

Answer:

jQuery is one of the widely used libraries of JavaScript due to its extremely easy syntax for performing tedious task like Ajax calls and DOM manipulation.

jQuery is a client scripting. It helps to build robust and modern web applications in very short span of time.

It executes inside your browser when you request a page.

22. Which method is used to set one or more style properties for selected elements in jQuery?

Answer:

In jQuery, CSS() method is used to set one or more style properties for selected elements.

This method sets or returns one or more style properties for the selected elements.

CSS() method helps to get the value of a computed style property for the first element in the set of matched elements or set one or more CSS properties for every matched element.

Example:

<!DOCTYPE html>
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jQuery/3.2.1/jQuery.min.js">
        </script>
        <script>
            $(document).ready(function(){
                $("button").click(function(){
                        $("p").css("color", "yellow");
                });
            });     
        </script>
    </head>
    <body>
        <p style="background-color:#ff0000">TutorialRide</p>
        <p style="background-color:#00ff00">TutorialRide</p>
        <p style="background-color:#0000ff">TutorialRide</p>
        <button>Change Text Color</button>
    </body>
</html>


css method

23. Which function is used to prevent code from running, before the document is finished loading?

Answer:

The onload event is used to prevent code from running, before the document is finished loading.

This event occurs when an object has been loaded.

It is most often used within the <body> tag to execute a script once a web page has completely loaded all content (including images, script files, CSS files, etc.).

This event can be used to check the visitor's browser type and browser version, and load the proper version of the web page based on the information.

Example:

<!DOCTYPE html>
<html>
    <body>
        <img src="/home/careerride2/Downloads/download.jpg" onload="loadImage()" width="500" height="600">
        <script>
            function loadImage()
            {
                alert("Image is loaded");
            }
        </script>
    </body>
</html>


onload event

24. Which method is used to switch between adding/removing one or more classes (for CSS) from selected elements?

Answer:

The switchClass method is used to adds and removes the specified classes to each of the set of matched elements while animating all style changes.

This method is used to move from one CSS class to another CSS class, animating the transition from one state to another state.

Syntax:
.switchClass( removeClassName, addClassName [, options ] )

Parameters,

removeClassName: This parameter is a string. It represents the CSS class name or space demarcated list of class names, to be removed.

addClassName: This parameter is of type string. It represents one or more class names which are added to the class attribute of each matched element.

options: It is used to specify all animation settings. All properties are optional. Its possible values are:

Duration: This is a string or number. It specifies how long the animation will run. Its default value is 400.

Easing: It is a string which specifies the easing function used for transition. Its default value is swing.

Complete: It is a callback method called for each element when the effect is completed for this element.

Children: This is a Boolean type. It specifies whether the animation should additionally be applied to all descendants of the matched elements. Its default value is FALSE.

Queue: This is of Boolean type or string type. It specifies whether to place the animation in the effects queue. Its default value is TRUE.

25. What are the advantages of CDN?

Answer:

CDN reduces the load from the server.

If a user regularly visits a site which is using jQuery frameworks from any of these CDN, it will be cached.

CDN saves bandwidth. jQuery framework is loaded faster from these CDN.

It reduces existing hosting cost and increases the performance.

CDN gives fast response time in delivering content to end users.