J Query Important Questions
J Query Important Questions
A lightweight JavaScript library which gives a quick and simple method for HTML DOM traversing and
manipulation, its event handling, its client-side animations, and so on.
1) What is jQuery?
jQuery is a lightweight JavaScript library which gives a quick and simple method
for HTML DOM traversing and manipulation, its event handling, its client-side
animations, and so on. One of the best features of jQuery is that jQuery supports an
efficient way to implement AJAX applications because of its lightweight nature and
make normalize and efficient web programs.
Example
$.connect(obj1, 'fun1', obj2, fun2);
Here we are binding fun2 function of object 2 to the fun1 function of object 1. So, when
fun2 is executed, fun1 will also be executed.
Example
$.noConflict
Code that uses $ object of other libraries can be written here without any conflict
between the $ (i.e), jQuery.
No. JQuery is not a W3C standard. It is just a JavaScript library that provides an
abstraction to the functions executed in JavaScript.
The filter method in the jQuery returns certain elements that match the specified
criteria. You can specify a criterion with the filter method and apply it to DOM elements.
It removes the elements that do not match the specified criteria and allows only that
matches. It works like a search function.
Syntax
$(selector).filter(criteria,function(index))
Here the criteria specify an expression that you want to apply to the DOM elements for
filtering. You can specify multiple criteria separated by a comma. The function is
optional that runs on each specified element. It returns true if the element matches the
criteria or false otherwise.
6) What are deferred and promise object in jQuery?
The promise is set on a deferred object that will be executed when the deferred
collection is complete. The state of the promise cannot be changed. The deferred object
is used to create a promise.
jQuery is very compact and well-written JavaScript code that increases the productivity
of the developer by enabling them to achieve critical UI functionality by writing very less
amount of code.
It helps to
Chaining in jQuery lets you run multiple statements one after another on the same
element. To chain multiple commands, append each jQuery command to the previous
one using dot(.). Chaining allows you not to use one selector more than once for
performing an action as all the actions are performed at once. It speeds up the time
taken to execute the code as the compiler need not call a selector more than one time.
Example
$(“#h1”).css(“color”, "blue”).slideUp(200).slideDown(100);
In the above statement, the color, slideup, and slidedown action are applied on the
heading selector in the same line using chaining.
As the name suggests, jQuery UI is a bunch of GUI widgets, animation effects, and
themes. It is implemented using jQuery, CSS, and HTML. It is an open-source
JavaScript library that is first released by the jQuery Foundation in 2007 and is now
under the MIT license. It is one of the popular JavaScript libraries that is used in some
of the popular websites for designs and animations like Pinterest, PayPal, Netflix, and
IMDb.
The jQuery() can also be written as $(). It is used as a selector for searching through the
DOM elements that match the provided selector. It searches within the DOM element at
the document root.
Example
$(“h1”).hide() ;
In the above statement, $() selects the h1 heading and applies the provided action. It
hides all the heading h1 tags in the document.
12) What are plugins in JQuery?
Plugin in jQuery is a file written in standard JavaScript code. This file contains some
methods that can be used with jQuery methods. These plugins can be downloaded from
the official repository of the jQuery plugins. The plugin can be included in the jQuery file
similar to other libraries in the head of the document. You can even create your own
plugin.
Example of a plugin
<head>
<script src = "jquery.plug-in.js" type = "text/javascript"></script>
</head>
The jQuery stop() function is used to stop all the animations currently running on the
selected elements. It stops the animation before it is finished.
Syntax
$(selector).stop(stopAll,goToEnd);
To completely disable elements globally, then set the jQuery.fx.off property to true. The
default value is false which lets run all the animations normally. Setting it true disables
all animations from all the elements immediately and sets the element to the final state.
Syntax
jQuery.fx.off = true|false;
Syntax
$(selector).slideDown(speed,callback);
Here the speed parameter specifies the duration of the slideDown effect such as “slow”,
“fast", or in milliseconds. The callback function is optional and is executed after the
animation slideDown is completed on the specified element.
Syntax
$(selector).slideUp(speed,callback);
The speed parameter specifies the duration and the optional callback function is
executed after the animation is completed.
SlideToggle() :This method is used to toggle between the slideUp() and slideDown()
method. If the element has been slid up, this method will slide them up and vice versa.
Syntax
$(selector).slideToggle(speed,callback);
The speed parameter specifies the duration of the effect. The callback function is an
optional function that is executed after the completion of the effect.
17) List different types of filters available in jquery?
The $.each() method is used to iterate over all the DOM elements in the jQuery object
and executes a function for each matched element. This method is used in manipulating
multi-element DOM.
Example
The holdReady() function in jQuery is used to delay the ready event. If you want to load
additional jQuery plugins before the execution of the DOM element even though they
are ready, this method is used. It is called early in the document in the tag. Events that
have already fired before calling this method will not be affected.
Example
$.holdReady( true );
$.getScript( "myplugin.js", function() {
$.holdReady( false );
});
Syntax
$(selector).clone(true|false)
The optional parameter true or false specifies whether to clone the event handler or not.
This method returns the cloned values of the selected elements.
Syntax
JQuery.type(object);
It returns the type of the object that is passed.
The below jQuery code is used to disable the back button in the browser.
<script type="text/javascript">
$(document).ready(function() {
window.history.pushState(null, "", window.location.href);
window.onpopstate = function() {
window.history.pushState(null, "", window.location.href);
};
});
</script>
23) What is event.PreventDefault in jQuery?
Syntax
event.preventDefault()
<script>
$(document).ready(function(){
$("a").click(function(event){
event.preventDefault();
});
});
</script>
Here, the preventDefault() method prevents the link from opening the URL.
CSS Selector
XPath Selector
Custom Selector
jQuery Selectors are used to select one or a group of HTML elements from your
web page.
jQuery support all the CSS selectors as well as many additional custom
selectors.
jQuery selectors always start with dollar sign and parentheses: $()
There are three building blocks to select the elements in a web document.
Example
$(div)
It will select all the div elements in the document.
2. Select elements by ID
Example
$(“#abc”)
It will select single element that has an ID of abc.
Example
$(“.xyzClass”)
It will select all the elements having class xyzClass.
In jQuery, there is two way to change the width of an element. One way is using
.css(‘width’) and other way is using .width().
For example
$('#mydiv').css('width','300px');
$('#mydiv').width(100);
The difference in .css(‘width’) and .width() is the data type of value we specify or
return from the both functions.
In .css(‘width’) we have to add “px” in the width value while in .width() we don’t
have to add.
When you want to get the width of “mydiv” element then .css(‘width’) will return
‘300px’ while .width() will return only integer value 300.
The bind() method will not attach events to those elements which are added after DOM
is loaded while live() and delegate() methods attach events to the future elements also.
For example
$(document).ready(function(){
$("#myTable").find("tr").live("click",function(){
alert($(this).text());
});
});
Above code will not work using live() method. But using delegate() method we can
accomplish this.
$(document).ready(function(){
$("#dvContainer")children("table").delegate("tr","click",function(){
alert($(this).text());
});
});
Syntax:
“boolValue” specifies whether to use the traditional style of param serialization or not.
Example
personObj=new Object();
empObject.name="Ravi";
empObject.age="28";
empObject.dept="IT";
$("#clickme").click(function(){
$("span").text($.param(empObject));
});
It will set the text of span to “name=Ravi&age=28&dep=IT”
To deal with cookies in jQuery we have to use the Dough cookie plugin.
Create cookie:
$.dough(“cookie_name”, “cookie_value”);
Read Cookie:
$.dough(“cookie_name”);
Delete cookie:
$.dough(“cookie_name”, “remove”);
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.
Ajax allows the user to exchange data with a server and update parts of a page without
reloading the entire page. Some of the functions of ajax are as follows:
$.ajax(): This is considered to be the most low level and basic of functions. It is used to
send requests . This function can be performed without a selector.
$.ajaxSetup(): This function is used to define and set the options for various ajax calls.
For example.
$.ajaxSetup({
"type":"POST",
"url":"ajax.php",
"success":function(data){
$("#bar")
.css("background","yellow")
.html(data);
}
});
Shorthand ajax methods: They comprise of simply the wrapper function that call
$.ajax() with certain parameters already set.
$.getJSON(): this is a special type of shorthand function which is used to accept the url
to which the requests are sent. Also optional data and optional callback functions are
possible in such functions.
Syntax:
$(selector).empty();
$(selector).remove();
$(selector).detach();
34) How can events be prevented from stopping to work after an ajax
request?
$('#mydiv').click(function(e){
if( $(e.target).is('a') )
fn.call(e.target,e);
});
$('#mydiv').load('my.html')
Event rebinding usage: When this method is used it requires the user to call the bind
method and the added new elements.
$('a').click(fn);
$('#mydiv').load('my.html',function(){
$('#mydiv a').click(fn);
});
We can have more than one document.ready() function in a page where we can
have only one onload function.
Document.ready() function is called as soon as DOM is loaded where
body.onload() function is called when everything gets loaded on the page that
includes DOM, images and all associated resources of the page.
36) What are features of JQuery or what can be done using JQuery?
Features of Jquery