jQuery Interview Questions (Part-3)


jQuery interview questions and answers

How can you get the type of arguments passed to a function?

Using typeof operator, we can get the type of arguments passed to a function.
if ( typeof value === 'string' ) { // it's a string } else { // it's something else }

How to select all elements using jQuery?

You can use $('*') to select all elements available in a DOM.

How to get attributes of an element using jQuery?

jQuery .attr() method is used for getting the value from the first matched element.

How can you apply a style on an element using jQuery?

jQuery css() method sets or returns one or more style properties for the selected elements.
$("p").css("background-color", "red");

How to get/set the html contents of an element using jQuery?

The innerHTML property sets or returns the HTML content (inner HTML) of an element.
document.getElementById("test").innerHTML = "Text Changed";

How do you add an HTML element in DOM tree?

The jQuery append() method inserts specified content at the end of the selected elements.

Define bind() and unbind elements in jQuery?

jQuery bind() function is used to attach an event handler to elements, while the unbind() is used to detached an existing event handler from elements.

What is the use of delay() method in JQuery?

The delay() method sets a timer to delay the execution of subsequent items in the queue.
$("#myDiv").delay("slow").fadeIn();

What is the jQuery code to select all links inside the paragraph?

$("p a").attr("href", "https://net-informations.com");

What is jQuery code to select all links in first paragraph?

var links = $('p:first a');

What is jQuery.noConflict?

By default, jquery uses the variable jQuery and the $ is used for your convenience. Many JavaScript libraries use $ as a function or variable name, just as jQuery does. If we need to use another JavaScript library alongside jQuery, we can return control of $ back to the other library with a call to $.noConflict() . Once you have used jQuery.noConflict , the control of $ is handed over to other libraries rather than jQuery and in this case, you will have to use jQuery rather than $.

Differentiate between calling stop (true, true) and finish method?

The jQuery stop(true, true) method will be quite similar to finish() method. Both methods helps in clearing the queue and thereby helps the animations to enter into their end state. In .finish() it also causes the CSS property of all queued animations to jump to their end values, as well. jQuery stop() method stops the currently running animation on the matched elements.

What does $("P") will select?

This will select all the P elements (Paragraph) on page.

What are the fastest selectors in jQuery?

ID and element selectors are the fastest selectors in jQuery.

How to disable jQuery animation?

The jQuery.fx.off property is used to globally disable/enable all animations.
jQuery.fx.off = true/false;

How to create clone of any object using jQuery?

jQuery clone() method makes a copy of selected elements, including child nodes, text and attributes.
$("button").click(function(){ $("div").clone().appendTo("body"); });

Will Events Are Also Copied On Clone In Jquery?

By Default clone method does not clone the events unless it is being instructed to copy. When "clone" method is being instructed to clone the events also then along with elements, events are also being copied. The clone() method takes a parameter, if you pass true then it will copy the events as well.
$('#myDiv').clone(true).appendTo('body');
A clone(true) indicating whether event handlers should be copied along with the elements.

How can set/change the page title in jQuery?

$(document).attr("title", "New Title");

Difference between remove(), empty()?

  1. empty() will remove all the contents of the selection.
  2. remove() will remove the selection and its contents.

What Is The Use Of Validation Jquery Plugins?

Validation Jquery Plugin makes simple clientside form validation easy. In MVC, it can use these Jquery Plugins in the form of rules like the following:
$('#MyControlId').rules("add", { required: true });

How to execute jQuery function after 10 seconds?

$(document).ready(function() { setTimeout(function() { $("#myButtin").trigger('click'); }, 10000); });

What is has() in JQuery?

The .has() method constructs a new jQuery object that have one or more elements inside them, that matches the specified selector. example
<p>jQuery <span>has()</span> method.</p> $(document).ready(function(){ $("p").has("span").css("background-color", "red"); });

How jQuery selectors are executed?

Your last selectors are always executed first.

example
$("p#elementID.myClass");
In the above code, jQuery will first find all the elements with the class name ".myCssClass" and after that, it will reject all the other elements which are not in "p#elementID".

What is the difference between eq() and get() methods in jQuery?

  1. jQuery .get() method returns an array of raw DOM elements. You can manipulate each of them by accessing its attributes and invoking its functions as you would on a raw DOM element.

  2. jQuery .eq() returns a jQuery object, meaning the DOM element is wrapped in the jQuery wrapper, which means that it accepts jQuery functions.

How to disable Browser Back button through jQuery?

$(document).ready(function(){ window.history.pushState(null, "", window.location.href); window.onpopstate = function() { window.history.pushState(null, "", window.location.href); }; });

Can we use jQuery to make ajax request?

Yes. jQuery can be used for making ajax request. The ajax() method is used to perform an AJAX (asynchronous HTTP) request.

What are the four parameters used for jQuery Ajax method?

  1. URL – Need to specify the URL to send the request.
  2. Type – Specifies type of request(Get or Post).
  3. Data – Specifies data to be sent to server.
  4. Cache – Whether the browser should cache the requested page.

Advantages Of Jquery in Asp.net web application?

  1. It's lightweight, easy and fast.
  2. Cross Browser Compatibility.
  3. Easy and Light-weight Ajax Application.
  4. Availability of various plug-in's.
  5. Microsoft and Intellisense support in Visual Studio 2008.
  6. Easy Integration with ASP.Net Ajax projects.