jQuery interview questions and answers – Part15

141. Explain slideToggle() effect.
-slideToggle() effect is used to give animated sliding effect to an element.
slideToggle([ duration] [, easing] [, callback])
• “duration” is the number specifying how long the animation will run.
• “easing” is the string which specify the function for the transition.
• “callback” is the function which we want to run once the animation is complete.
• If the element is visible then this effect will slide the element up side and make it completely hidden. If the element is hidden then slideToggle() effect will slide it down side and make it visible.
• We can specify the toggle speed with this effect.
For example

$("#mydiv").slideToggle(“slow”, function(){
//run after the animation is complete.

142. What is difference between $(this) and ‘this’ in jQuery?
Refer the following example


-this and $(this) references the same element but the difference is that “this” is used in traditional way but when “this” is used with $() then it becomes a jQuery object on which we can use the functions of jQuery.
-In the example given, when only “this” keyword is used then we can use the jQuery text() function to get the text of the element, because it is not jQuery object. Once the “this” keyword is wrapped in $() then we can use the jQuery function text() to get the text of the element.

143. What is the use of param() method.
• The param() method is used to represent an array or an object in serialize manner.
• While making an ajax request we can use these serialize values in the query strings of URL.
• Syntax: $.param(object | array, boolValue)
• “object | array” specifies an array or an object to be serialized.
• “boolValue” specifies whether to use the traditional style of param serialization or not.
For example:

personObj=new Object();

It will set the text of span to “name=Arpit&age=24&dep=IT”

144. What is jQuery.holdReady() function?
-By using jQuery.holdReady() function we can hold or release the execution of jQuery’s ready event.
-This method should be call before we run ready event.
-To delay the ready event, we have to call
-When we want to release the ready event then we have to call
-This function is helpful when we want to load any jQuery plugins before the execution of ready event.
For example

$.getScript("xyzplugin.js", function() {

145. Explain .empty() vs .remove() vs .detach().
-.empty() method is used to remove all the child elements from matched elements.
-.remove() method is used to remove all the matched element. This method will remove all the jQuery data associated with the matched element.
-.detach() method is same as .remove() method except that the .detach() method doesn’t remove jQuery data associated with the matched elements.
-.remove() is faster than .empty() or .detach() method.

146. How to read, write and delete cookies in jQuery?
-To deal with cookies in jQuery we have to use the Dough cookie plugin.
-Dough is easy to use and having powerful features.
-Create cookie
$.dough(“cookie_name”, “cookie_value”);
Read Cookie
Delete cookie
$.dough(“cookie_name”, “remove”);

147. What is difference between sorting string array and sorting numerical array in jQuery?
The sort method is used to sort any array elements. It sorts the string elements alphabetically.
For example

var mylist = [ “Apple”,”Orange”,”Banana”];
mylist = mylist.sort();

It will give following output
Now we declare a numerical array and use sort() method to sort its elements.
var mylist = [ “20”,”3””100”,”50”];
mylist = mylist.sort();
It will give following output

148. What is difference between prop and attr?
• In jQuery both prop() and attr() function is used to set/get the value of specified property of an element.
• The difference in both the function is that attr() returns the default value of the property while the prop() returns the current value of the property.
For example

<input value="My Value" type="text"/> 
$('input').prop('value', 'Changed Value');
-.attr('value') will return 'My Value' 
-.prop('value') will return 'Changed Value'

149. How to always reference latest version of jQuery?
When you reference the jQuery on your web page, you have to specify the version number also.
<script type=”text/javascript”
Above code will always load the 1.5.1 version of jQuery. If you reference the latest jQuery then you don’t need to change the code every time the new version of jQuery is released.
To achieve this you have to use following code
<script type=”text/javascript”
This code will always reference the latest version of jQuery in your page.

150. What is resize() function in jQuery?
The resize() function is called whenever the browser size is changed. This event can be only used with $(window).
.resize([event_data], handler(event_object))
-The “event_data” is the data to be sent to the handler.
-The “handler(event_object)” is a function to be called each time when the window is resized.
For example

$(window).resize(function() {
$('#message).text('window is resized to ' + $(window).width() + ‘x’ + $(window).height());
Sign-up for our email newsletter and get free job alerts, current affairs and GK updates regularly.
Subscribe Here (or)

Leave a Reply

Your email address will not be published. Required fields are marked *

seven + 7 =

Popup Dialog Box Powered By : XYZScripts.com
  • RSS
  • Facebook
  • Google+
  • Twitter