Home
Jquery
Interview FAQs

FAQs of level

The .animate() method allows us to create animation effects on any numeric CSS property. This method changes an element from one state to another with CSS styles. The CSS property value is changed gradually, to create an animated effect.

Syntax:

(selector).animate({styles},speed,easing,callback)
styles: Specifies one or more CSS properties/values to animate.
duration: Optional. Specifies the speed of the animation.
easing: Optional. Specifies the speed of the element in different points of the animation. Default value is "swing".
callback: Optional. A function to be executed after the animation completes.
Simple use of animate function is,
$("btnClick").click(function(){
$("#dvBox").animate({height:"100px"});
});

Using jQuery property "jQuery.fx.off", which when set to true, disables all the jQuery animation. When this is done, all animation methods will immediately set elements to their final state when called, rather than displaying an effect.

 $("<div/>") : This creates a new div element. However this is not added to DOM tree unless you do not append it to any DOM element.

$("div") : This selects all div elements present on the page.

Using jQuery ".stop()" method  you stop the currently-running animation.

All these methods .empty(), .remove() and .detach() are used for removing elements from DOM but they all are different.
.empty(): This method removes all the child element of the matched element where remove() method removes set of matched elements from DOM.
.remove(): Use .remove() when you want to remove the element itself, as well as everything inside it. In addition to the elements themselves, all bound events and jQuery data associated with the elements are removed.
.detach(): This method is the same as .remove(), except that .detach() keeps all jQuery data associated with the removed elements. This method is useful when removed elements are to be reinserted into the DOM at a later time.

All these 4 jQuery methods are used for attaching events to selectors or elements. But they all are different from each other.

.bind(): This is the easiest and quick method to bind events. But the issue with bind() is that it does not work for elements added dynamically that matches the same selector. bind() only attach events to the current elements not future element. Above that it also has performance issues when dealing with a large selection.
.live(): This method overcomes the disadvantage of bind(). It works for dynamically added elements or future elements. Because of its poor performance on large pages, this method is deprecated as of jQuery 1.7 and you should stop using it. Chaining is not properly supported using this method.
.delegate(): The .delegate() method behaves in a similar fashion to the .live() method, but instead of attaching the selector/event information to the document, you can choose where it is anchored and it also supports chaining.
.on(): Since live was deprecated with 1.7, so new method was introduced named ".on()". This method provides all the goodness of previous 3 methods and it brings uniformity for attaching event handlers.

jQuery provides clone() method which performs a deep copy of the set of matched elements, meaning that it copies the matched elements as well as all of their descendant elements and text nodes.
$(document).ready(function(){
$("#btnClone").click(function(){
$("#dvText").clone().appendTo("body");
return false;
});
});

Using clone() method, we can create clone of any element but the default implementation of the clone() method does not copy events unless you tell the clone() method to copy the events. The clone() method takes a parameter, if you pass true then it will copy the events as well.

$(document).ready(function(){
$("#btnClone").bind("click", function(){
$("#dvClickme").clone(true).appendTo("body");
});

The difference between prop and attr are-

  • attr(): Get the value of an attribute for the first element in the set of matched elements. Whereas ,.prop(): (Introduced in jQuery 1.6) Get the value of a property for the first element in the set of matched elements.
  • attr() gives you the value of element as it was defines in the html on page load. It is always recommended to use prop() to get values of elements which is modified via JavaScript/jQuery.

The event.preventDefault() method stops the default action of an element from happening. 
PreviousDisplaying 6 of 7Next
1 2 3 4 5 6 7
Need Help? Contact Us.

Log in

*
*

Forgot password?

*

New User

*
*
*
*