Category Archives: Jquery

.slideUp() Effect in Jquery

Description:

Jquery slideUp() method is used to slide UP the matched element with a sliding motion.

Syntax

.slideUp( duration, easing, complete/callback )

Ex :

$("#div").slideUp("slow", function() 
{ 
// Execute after Animation completed.
});

Duration/speed  It specifies the speed of the fadein. The valuse may be slow, fast and milliseconds. (default value: 400). Duration are given in milliseconds and higher values indicate slower animations, not faster ones.
Easing  It specifies the easing function to be used for transition (default value: swing).
Callback A function to call once the animation is complete.

Example:

From the below code, slideUp() method will slide UP the selected/matched element and finally “display:none” will append to the div to make it hidden. In other word, the .slideUp() method animates the height of the matched elements. Once the height reaches 0 then the display” style property is set to “none”.

<html>
<head>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
 <script type="text/javascript">

 $(document).ready(function () {
 $("#divflipBox").click(function () {
 $("#divpanelBox").slideDown("slow");
 });
 });

 </script>
 <style type="text/css">
 #divflipBox, #divpanelBox{
 background-color:#a0ce4e;
 border:solid 1px #c3c3c3;
 width:25%;
 }
 #divpanelBox{
 padding: 30px; 
 display: none;
 }

 </style>
</head>
<body>

 <div id="divflipBox">
 Click to slide down</div>
 <div id="divpanelBox">
 Hello Welcome to dotnet-helpers.com
 Learn MVC, ASP , JQuery with best examples. Thanks for visiting.
 </div> 
</body>
</html>

Output:

After clicking the “Click to slide Up” button, then the “divpanelBox” will slideUp with animation. Finally “display:none” will append to the div to make it hidden.

.slideDown() Effect in Jquery

Description:

Jquery slideDown() method slide down the matched elements with a sliding motion.

Syntax

.slideDown( duration, easing, complete/callback )

Ex :

$("#div").slideDown("slow", function() 
{ 
// Animation complete.
});

Duration/speed  It specifies the speed of the fadein. The valuse may be slow, fast and milliseconds. (default value: 400). Duration are given in milliseconds and higher values indicate slower animations, not faster ones.
Easing  It specifies the easing function to be used for transition (default value: swing).
Callback A function to call once the animation is complete.

Example:

From the below code, slideDown() method will apply slide down to the selected element and apply “display:block” to the element. The .slideDown() method animates the height of the matched elements. This causes lower parts of the page to slide down, making way for the revealed items.

<html>
<head>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
 <script type="text/javascript">

 $(document).ready(function () {
 $("#divflipBox").click(function () {
 $("#divpanelBox").slideDown("slow");
 });
 });

 </script>
 <style type="text/css">
 #divflipBox, #divpanelBox{
 background-color:#a0ce4e;
 border:solid 1px #c3c3c3;
 width:25%;
 }
 #divpanelBox{
 padding: 30px; 
 display: none;
 }

 </style>
</head>
<body>

 <div id="divflipBox">
 Click to slide down</div>
 <div id="divpanelBox">
 Hello Welcome to dotnet-helpers.com
 Learn MVC, ASP , JQuery with best examples. Thanks for visiting.
 </div> 
</body>
</html>

Output:

After clicking the “Click to slide down” button, then the “divpanelBox” will slide down with animation. Finally “display:block” will append to the div to make it visible.

fadeTo() Effect in Jquery

Description:

jQuery fadeTo() method is used to fading the given opacity for the matched/selected element. It is similar to the .fadeIn method but that method unhides the element and always fades to 100% opacity.

Syntax

.fadeTo( duration, opacity

[, complete ] )

Ex : $(“#div”).fadeToggle(“slow”);

Duration/speed  It specifies the speed of the fadein. The valuse may be slow, fast and milliseconds. (default value: 400). Duration are given in milliseconds and higher values indicate slower animations, not faster ones.
Easing  It specifies the easing function to be used for transition (default value: swing).
Callback A function to call once the animation is complete.

Example:

From the below code, fadeTo() method will apply opacity of 0.2 to the divFadeBox element. A number between 0 and 1 specifying the target opacity. 

<html>
<head>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
 <script type="text/javascript">

 $(document).ready(function () {
 $("#divButton").click(function () {
 $("#divFadeBox").fadeTo("slow", 0.2);
 });
 });

 </script>
</head>
<body>
 <button id="divButton">Click to Apply Fadding using .fadeTo()</button><br><br>
 <div id="divFadeBox" style="width:80px;height:80px;background-color:blue;"></div>
</body>
</html>

Output:

After clicking the “Click to Apply Opacity using .fadeTo()” button, then the blue box will be fadein up to specified opacity (ie., 0.2) as shown below

Before button click:

<div id="divFadeBox" style="width: 80px; height: 80px; background-color: blue;"></div>

After button click:

<div id="divFadeBox" style="width: 80px; height: 80px; background-color: blue; opacity: 0.2;"></div>

 

.fadeToggle() Effect in Jquery

Description:

jQuery fadeToggle() method is used to toggle between the fadeIn() and fadeOut() methods. If the selected/matched elements are fadedIn then the fadeToggle will make them faded out. And if the selected elements are fadedOut then the fadeToggle will make them fadeIn.

Syntax

.fadeToggle(

[duration ] [, easing ] [, complete ] )

Ex : $(“#div”).fadeToggle(“slow”);

Duration/speed  It specifies the speed of the fadein. The valuse may be slow, fast and milliseconds. (default value: 400)
Easing  It specifies the easing function to be used for transition (default value: swing).
Callback A function to call once the animation is complete.

Example:

From the below code, the “divFadeBox” been visible during the page load. After clicking “divButton”  the click event been triggered and the fadeToggle() will be executed and the box will be disappear. For more explanation see below output section

<html>
<head>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
 <script type="text/javascript">

 $(document).ready(function () {
 $("#divButton").click(function () {
 $("#divFadeBox").fadeToggle();
 });
 });
 

 </script>
</head>
<body>
 </br>
 <button id="divButton">Click to fade Toggle boxes</button><br><br>
 <div id="divFadeBox" style="width:80px;height:80px;background-color:blue;"></div>
</body>
</html>

Output:

After clicking the “Click to fade Toggle boxes” button, then the blue box will be disappear if  it is in visible state else it will be appear if it is in hide like shown below.

.fadeOut() Effect in Jquery

Description:

The fadeOut( ) method fades out all selected/matched elements by applying the opacity to 0, then setting display to “none” . Finally the optional callback will be triggered after completion of animation.

Syntax

.fadeOut(

[duration ] [easing] [callback] )

Ex : $(“#div”).fadeOut(“slow”);

Duration/speed  It specifies the speed of the fadein. The valuse may be slow, fast and milliseconds. (default value: 400)
Easing  It specifies the easing function to be used for transition (default value: swing).
Callback A function to call once the animation is complete.

Example: Without Option(parameter)

From the below code, the “divFadeBox” been visible on the initial load. After clicking “divButton”  the click event been triggered and the fadeOut() will be executed on the “divFadeBox”. Finally “display:none” will be set to the matched element.

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function () {
$("#divButton").click(function () {
$("#divFadeBox").fadeOut();
});
});

</script>
</head>
<body>
</br>
<button id="divButton">Click to fade in boxes</button><br><br>
<div id="divFadeBox" style="width:80px;height:80px;display:none;background-color:blue;"></div>
</body>
</html>

Output:

After clicking the “Click to fade Out boxes” button, then the blue box will be disappear as shown below.

<div id="divFadeBox" style="width: 80px; height: 80px; background-color: blue;"></div>

<div id="divFadeBox" style="width: 80px; height: 80px; display: none; background-color: blue;"></div>

fadeIn() Effect in Jquery

Description:

Display matched elements by fading them to opaque. In simple word, its animates the opacity for the matched elements.

Syntax

.fadeIn(

[duration ] [easing] [callback] )

$(“#div”).fadeIn(“slow”);

Duration/speed  It specifies the speed of the fadein. The valuse may be slow, fast and milliseconds. (default value: 400)
Easing  It specifies the easing function to be used for transition (default value: swing).
Callback A function to call once the animation is complete.

Example: Without Option(parameter)

From the below code, the “divFadeBox” element has been set to display:none. So initially it will not visible during the page load. After clicking “divButton”, the fadeIn() will be executed to the “divFadeBox” element.

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function () {
$("#divButton").click(function () {
$("#divFadeBox").fadeIn();
});
});

</script>
</head>
<body>
</br>
<button id="divButton">Click to fade in boxes</button><br><br>
<div id="divFadeBox" style="width:80px;height:80px;display:none;background-color:blue;"></div>
</body>
</html>

Output: 

After clicking “Click to fade in boxes” button, then the blue box will be fadeIn as shown below.

Example : With Option(parameter)

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function () {
$("#divButton").click(function () {
$('#divFadeBox').fadeIn(2000, 'swing', function () {
//Execute(callback) function after animation completed.
$("#divButton").attr('value', 'fadeIn() is now Complete');});
}); 
});

</script>
</head>
<body>
<input id="divButton" type="button" value="Click here to see fadeIn" /><br><br>
<div id="divFadeBox" style="width:80px;height:80px;display:none;background-color:blue;"></div>
</body>
</html>

Output

Before button click

After button click, it will be fade-in with duration of 2000 milli second & swing effect. After fade-in completed, callback method will be executed. Finally “fadein() is now complete” text will be append to the button button as shown below.

$(“#divButton”).attr(‘value’, ‘fadeIn() is now Complete‘);

 

jQuery Effects

jQuery framework provide more flexible way to add more effects on a web page by using in-build methods. jQuery effects can be categorized in to following ways

Short Idea About Jquery Effects:

jQuery Effects provide large number of methods for developing rich web application. Let we discuss about jQuery effect below.

Method Description
hide() hides the selected elements.
show() displays the selected elements.
toggle() shows or hides the matched elements.
fadein() Display the matched elements by fading them to opaque.
fadeout() Hide the matched elements by fading them to transparent.
fadeto() Adjust the opacity of the matched elements.
fadetoggle() shows/hides the selected element. ie., toggles between the fadeIn() and fadeOut().
slidedown() shows the matched elements with sliding motion.
slidetoggle() shows or hides the matched elements with slide. ie., it is used to toggle between the slideUp() and slideDown().
slideup() hides the matched elements with sliding motion.
animate() performs animation for the selected element.
clearQueue() It is used to remove all remaining queued functions from the selected elements.
delay() sets delay execution for all the queued functions on the selected elements.
dequeue() It is used to remove the next function from the queue, and then execute the function.
finish() It stops, removes and complete all queued animation for the selected elements.
queue() Show or manipulate the queue of functions to be executed on the matched elements.
stop() stops the animation on the selected elements.

 

Let us discuss more about effect one by one detailed way in up-coming posts.

Implementing jQuery.ajax() using Jquery

What is JQuery Ajax?

AJAX is standing for Asynchronous JavaScript and XML. It helps us to fetch the information from the server without reloading the page (ie., without refresh)JQuery is a great tool which provides a rich set of AJAX methods to develop great web application. jQuery provides the $.ajax method and several methods to make it easier to work with XHRs across browsers.

$.ajax

We can use the jQuery $.ajax() method in a different ways, let we discuss one by one.

  • we can pass it a configuration object as its argument
  • we can pass it a URL and an optional configuration object

SYNTAX: 

  • $.ajax(url,[options])
  • $.ajax([options])

Option Parameter:

Here i have listed few parameters which is using frequently.

Options Description
accepts The content type sent in the request header that tells the server what kind of response it will accept in return.
async By default, all requests are sent asynchronously. Set it false to make it synchronous.
beforeSend A callback function to be executed before Ajax request is sent.
cache A boolean indicating browser cache. Default is true.
complete A callback function to be executed when request finishes.
contentType A string containing a type of content when sending MIME content to the server.
data A data to be sent to the server. It can be JSON object, string or array.
dataType Type of data that we receiving back from the server.
error A callback function to be executed when the request fails.
headers An object of additional header key/value pairs to send along with request.
isLocal Allow the current environment to be recognized as local.
statusCode A JSON object containing numeric HTTP codes and functions to be called when the response has the corresponding code.
success A callback function to be executed when Ajax request succeeds.
timeout A number value in milliseconds for the request timeout.
type A type of http request for operation e.g. POST, PUT and GET. Default is GET.
url A string containing the URL to which the request is sent.

 

1) we can pass it a configuration object as its argument:

The $.ajax() method offers the ability to specify both success and failure callbacks. It have a felxibility
to take a configuration object, which can be defined separately. This approach make the developer to write reusable code.

var updateSuccessfully = function( obj ) {
$( '#divSuccess').html( "Updated successfully" );
};

var showError = function( req, status, err ) {
console.log( 'Went wrong while performing operation', status, err );
};

var ajaxUpdateRequest = {
url: '/dotnethelpers/UpdateUsers',
dataType: 'json',
success: updateSuccessfully,
error: showError
};

// Initiate the request
$.ajax(ajaxUpdateRequest);

Code Explanation:

updateSuccessfully,showError -: Both are methods created for handling success & error.
ajaxUpdateRequest :  It is declared as a separate method for handling the ajax operation. Inside the code, if it return success then ajax success will call the updateSuccessfully method and showError method when ajax operation fails. This approach make the developer to write reusable code.

2) we can pass it a URL and an optional configuration object

In this type, we can also call the $.ajax() method by passing it a URL and an optional configuration object.
This is very useful if we want to use the default configuration for $.ajax(), or if you want to use the same configuration for several URLs

$.ajax( '/dotnethelpers/UpdateUsers', {
type: 'GET',
dataType: 'json',
success: function( resp ) {
console.log( "Operation Performed Successfully" );
},
error: function( req, status, err ) {
console.log( 'Went wrong while performing operation',, status, err );
}
});

Code Explanation:

In above code, we can take advantage of passing url, type, datatype etc as an parameter. so it can be used by several other method for performing operation with different arguments.

 

jQuery Events Handling

What is Event?

Events are actions that can be used in the application. The events can be bind using jquery methods like .on, .bind,etc.,. Below are the few examples for events

  • Mouse click
  • Mouse over on an Element
  • Key Enter
  • An HTML form submission

When these events are triggered, we can use our own custom function to handle the event. These custom functions called as Event Handlers.

Types of Events:

jQuery provides a number of  shorthand Event methods and each corresponds to a DOM event. 

Event Name Shorthand Method
click .click()
keydown .keydown()
keydown .keypress()
keyup .keyup()
mouseover .mouseover()
mouseout .mouseout()
mouseenter .mouseenter()
mouseleave .mouseleave()
scroll .scroll()
focus .focus()
blur .blur()
resize .resize()

Binding Event Handler :

Let us discuss about event handling with simple examples.

Example 1: Simple event binding

$( “div” ).on( “click”, function() {
alert( “div clicked by user” );
});

Explanation :

In the above example, the method is to handle the click event for the div. If user click the div, then click function will automatically triggered
and alert dialog box will appear with the message “div clicked by user”.

Example 2: Handling more than one events

We can handle more than one event in the single function as shown below. 

$( “div” ).on( “mouseenter mouseleave”, function() {
alert( “User using mouse-enter/leave” );
});

Explanation :

The event will trigger after user mouseover/mouseleave on the div.

jQuery DOM Traversing

JQuery is a powerful framework which provides very flexible DOM traversal methods to help us by selecting the elements. We can select the element by two ways namely random and sequential method. In simple word, we can traverse deeper into the DOM with the help of query traversing methods. Mainly traversing can be categorized in three ways. They are,

Parent traversing

The jQuery methods used for finding their parents elements are,

[su_table]
 .parent() selects only one element in the DOM tree.
 .parents() It will function same like parent() method but it select all the element up to the DOM tree
 .parentsUntil() It will traverse and find ancestors of each element in the current set of matched elements
 .closest(). selects the first element that matches the selector

[/su_table]

Children traversing

. Childrent () and.Find () methods are used for finding the child elements from the selection. The main difference between these two methods depend up on how far they traversing is made.

[su_table]
 .Children()  Find on direct child nodes
 .find() It can traverse in to children, children of those children so on. It operate recursively.

[/su_table]

Siblings traversing

The last traversal method is siblings. The siblings (each of two or more children or offspring having one or both parents in common; a brother or sister.)  methods traversal as the name implies by its.

[su_table]
.prev() It will find one element previous to selected element
.next() It will find one element next to selected element
.siblings() It will perform both the .prev and  .next operation
.nextAll() It will select all the element next to selected object

[/su_table]

We can find previous elements by using.Prev() method & next elements with .Next (), and both with .Siblings().

Note:

  • Traversal methods do not alter/modify the objects. They’re mainly used to filter the elements of  DOM object.

Let us discuss details about all traverse method with examples in up-coming posts.

 

Keep Cool Coding…