Interview Ques & Ans For Jquery
Interview Ques & Ans For Jquery
There are a few differences between jQuery and other libraries. The following
makes jQuery lightweight and easier to understand, integrate, and use.
When using jQuery Mobile, you can use the library to create a mobile-first app that
is faster and easier to maintain than traditional web apps. It also makes it easier to
add new features to your app without having to rewrite your code.
JQuery Mobile is built on top of the jQuery JavaScript library. It works by using the
same techniques that are used when you are developing a traditional web app.
However, instead of using JavaScript, jQuery Mobile uses HTML5 and CSS3 to create
a modern and easy-to-use framework for developing mobile apps.
The $() function is used to access the properties of elements in the DOM
(Document Object Model). $() is similar to javascript’s selector functions, but
it is more powerful and has more options.
$() can be used to access attributes, classes, id, data-* attributes, and more.
Example - Suppose you want to change the colour of all the heading1 (h1) to
green, then you can do this with the help of jQuery as -
$(document).ready(function() {
$("h1").css("background-color", "green");
});
The $(document).ready() function is useful for loading scripts or styles into the
document at any time. It's not useful for loading scripts or styles into the document
when the document is already loaded.
onload() document.ready()
The onload() method is used to The document.ready() method is used to load
load the HTML code. the JavaScript code.
The onload() method loads the
The document.ready() method loads the
HTML code using a pre-compiled
JavaScript code using a pre-compiled object that
object that is passed to the onload
is passed to the document.ready event handler.
event handler
Some of the methods are listed below which provide the effect:
1. toggle() :
2. slideDown() :
This function is used to either check the visibility of selected elements or to show
the hidden elements. We can use this function on the following types of hidden
elements:
o Elements that are hidden using jQuery methods.
o Elements that are hidden using display: none in the element’s CSS properties.
Syntax: $(selector).slideDown( speed, easing, callback )
3. fadeOut():
This function is used to change the level of opacity for element of choice from
visible to hidden. When used, the fadded element will not occupy any space in DOM.
Syntax: $(selector).fadeOut( speed, easing, callback )
4. fadeToggle():
This is used for toggling between the fadeIn() and fadeOut() methods.
o If elements are faded in state, fadeToggle() will fade out those elements.
o If elements are faded out, fadeToggle() will fade in those elements.
Syntax: $(selector).fadeToggle(speed, easing, callback)
5. animate():
The method performs custom animation of a set of CSS properties. 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) where “styles” is a
required field that specifies one or more CSS properties/values to animate. The
properties need to be mentioned in camel casing style.
The parameters “speed”, “easing” and “callback” in the syntaxes of the above
methods represent:
o speed: Optional parameter and used for specifying the speed of the effect. The
default value is 400 milliseconds. The possible value of speed are “slow”, “fast” or
some number in milliseconds.
o easing: Again optional parameter is used for specifying the speed of elements to
different types of animation. The default value is “swing”. The possible value of
easing are “swing” and “linear”.
o callback: Optional parameter. The callback function specified here is
executed after the effect method is completed.
The hide() function in jQuery is used to try and hide the chosen element.
For Example - Suppose we need to hide a division tag, that consists of id =
“ib”. Then the jQuery code will be - $(“#ib”).hide();
User actions on a webpage are called events and handling responses to those is
called event handling. jQuery provides simple methods for attaching event handlers
to selected elements. When an event occurs, the provided function is executed.
jQuery.length property is used to count the number of the elements of the jQuery
object.
jQuery provides a method click() method that aids to trigger the click event.
For example, $(“p”).click() will trigger the click event whenever the elements with
paragraph tag are clicked on a browser page.
Syntax:
$(selector).click(function(){
});
$("p").each(function() {
console.log( $(this).attr('id'));
});