jQuery interview questions and answers – Part13

121. How to create clone of any object using jQuery?
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.

return false;

122. Does events are also copied when you clone any element in jQuery?
As explained in previous question, using clone() method, we can create clone of any element but the default implementation of the clone() method doesn’t 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.

$("#btnClone").bind('click', function(){

123. What is difference between prop and attr?
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.
Attributes carry additional information about an HTML element and come in name=”value” pairs. Where Property is a representation of an attribute in the HTML DOM tree. once the browser parse your HTML code ,corresponding DOM node will be created which is an object thus having properties.
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 , as it gives you the original value of an element’s current state. Find out more here.

124. What is event.PreventDefault?
The event.preventDefault() method stops the default action of an element from happening.

125. What is the difference between event.PreventDefault and event.stopPropagation?
event.preventDefault(): Stops the default action of an element from happening.
event.stopPropagation(): Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event. For example, if there is a link with a click method attached inside of a DIV or FORM that also has a click method attached, it will prevent the DIV or FORM click method from firing.

126. What is the difference between event.PreventDefault and “return false”?
e.preventDefault() will prevent the default event from occurring, e.stopPropagation() will prevent the event from bubbling up and return false will do both.

127. What is the difference between event.stopPropagation and event.stopImmediatePropagation?
event.stopPropagation() allows other handlers on the same element to be executed, whileevent.stopImmediatePropagation() prevents every event from running. For example, see below jQuery code block.

// This function won't be executed
$(this).css("background-color", "#f00");

If event.stopPropagation was used in previous example, then the next click event on p element which changes the css will fire, but in case event.stopImmediatePropagation(), the next p click event will not fire.

128. How to check if number is numeric while using jQuery 1.7+?
Using “isNumeric()” function which was introduced with jQuery 1.7.

129. How to check data type of any variable in jQuery?
Using $.type(Object) which returns the built-in JavaScript type for the object.

130. How do you attach a event to element which should be executed only once?
Using jQuery one() method. This attaches a handler to an event for the element. The handler is executed at most once per element. In simple terms, the attached function will be called only once.

$(document).ready(function() {
$("#btnDummy").one("click", function() {
alert("This will be displayed only once.");
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 *

six − = 1

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