JQUERY is a javascript library that simplifies the interaction with HTML documents, providing access to its elements, events, interactions with style sheets, effects, animation, and integration with AJAX.
A JQUERY code example could be this:
$("p").click(function()
$(this).hide();
});
});
Example of an HTML document where we use JQUERY
1.- JQUERY Load
How to load JQUERY
There are two ways to start using JQUERY:
- Download this Seller JQUERY from jquery.com
- Bind it from the internet, for example, from GOOGLE
1.1.- Download this Seller JQUERY from jquery.com
Once downloaded the latest version of JQUERY file, eg "jquery-1.10.2.min.js", copy the file in our website, and we reference in the HTML document as follows:
1.2.- Bind it from the internet, for example, from GOOGLE
If you want to download the file, you can reuse that are available online GOOGLE and MICROSOFT.
This way, you only need to include the following line in the HTML document to use JQUERY:
2.- JQUERY Syntax
From JQUERY is easy to access to any HTML element and perform some action on that item.
The JQUERY syntax is
JQUERY Examples
2.1.- If we want to hide all the <p> paragraphs in a document
2.2.- If we want to show all the <p> paragraphs of the HTML document
2.3.- If you want to hide all HTML elements with "class" equals to "black"
2.4.- If you want to hide all HTML elements with "id" equals to "black"
2.5.- If you want to hide the current item
3.- JQUERY Position
The JQUERY code can be placed in 2 different positions:
- JQUERY code placed in the same HTML document
- JQUERY code placed in a separate file
3.1.- JQUERY code placed in the same HTML document
JQUERY source code is placed directly in the head section of the HTML document.
Example HTML document JQUERY code:
Explaining the example
The document indicates where the library JQUERY is
JQUERY source ed between <script> and </script>
$("button").click(function()
To indicate that the JQUERY code will run when the HTML page is fully loaded in the browser (recommended).
To indicate that you are performing a function to "click" on the "button"
To indicate that it will hide all <p> paragraphs of HTML document
2.- JQUERY code placed in a separate file
To make it easier to keep our JQUERY code, we are placed in a separate file from the HTML file.
For example JQUERY place our code in the file "jquery1.js"
Now in our HTML page, we refer to our JQUERY file created before.
4.- JQUERY Selectors
The are used to the element or elements on which to perform an action.
To an item or items, you can use any CSS ors, such as: identifiers (id), classes (class), tags (h1, h2, p), attributes, attribute values.
JQUERY Selectors Examples
4.1.- To all the <p> tags in the HTML document
HTML code
JQUERY code to those elements
4.2.- To the elements with id = "dat", use "#"
HTML code
JQUERY code
4.3.- To elements with class = "dat", use "."
HTML code
JQUERY code
4.4.- To all items in an HTML document, use "*"
HTML code
<p>This is the first paragraph </p>
<p id="2">This is the second paragraph </p>
JQUERY code
4.5.- To the current item, use "this"
HTML code
JQUERY code
4.6.- To items with a "href" attribute, use "[]"
HTML code
JQUERY code
4.7.- To all elements that are empty, use "empty"
HTML code
<p></p>
JQUERY code
4.8.- To all elements of type = "text"
HTML code
JQUERY code
5.- JQUERY Events
An event is an action performed by the user on any element of the HTML document.
Types of events:
- Events held with the mouse : click, dblclick, mouseenter, mouseleave
- Events held with the keyboard : keypress, keydown, keyup
- Events held on a form element types : submit, change, focus, blur
- Past events on the document or the window : load, resize, scroll, unload
Examples
Defining the function to perform when you click on a label <p>
If we make doubleclick on a <p> item, hide the element.
HTML code where the javascript is included:
6.- JQUERY show
The show effect can display anything HMTL
Example JQUERY show
To display all elements <p>
It also allows the parameters "speed" and "callback"
The parameter "speed" indicates the milliseconds that it should take effect.
The parameter "callback" indicates JQUERY function to be executed at the end of the effect.
Example JQUERY show (speed, callback)
To show all <p> elements with a duration of 3 seconds and an alert that has completed the effect
7.- JQUERY hide
The hide effect to hide any HTML element
Example JQUERY hide
To hide all elements <p>
You can have 2 optional parameters (speed and callback)
The parameter "speed" indicates the milliseconds that it should take effect.
The parameter "callback" indicates JQUERY function to be executed at the end of the effect.
Example JQUERY hide (speed, callback)
To hide all <p> elements, with a duration of 2 seconds and an alert showing that the effect has ended
8.- JQUERY toggle
The toggle effect toggles between hide effect and show effect.
That is, if the item is hidden then show it.
And conversely, if visible, hidden.
Example JQUERY toggle
To hide an <p> element that is visible or hide a <p> element that is hidden
It also allows the parameters include "speed" and "callback"
9.- JQUERY fade
For the fade effect there are 4 possibilities: fadeIn, fadeOut, fadeToggle and fadeTo
9.1.- FadeIn
The fadeIn() effect allows smoothly view any element of the HTML document. At the end of the effect, the result is the same as the effect show().
Example JQUERY fadeIn() To hide all elements
You can have 2 optional parameters (speed and callback)
The parameter speed indicates the milliseconds that it should take effect.
The parameter callback indicates JQUERY function to be executed at the end of the effect.
Example JQUERY fadeIn (speed, callback)
To hide all <p> elements, with a duration of 2 seconds and display an alert that it has completed the effect
2000,
function() { alert("Now the text is already displayed");}
);
9.2.- FadeOut
The fadeOut() effect can blur an item until it is invisible. At the end of the effect, the result is like the hide() effect.
Example JQUERY fadeOut() to hide all <p> elements
You can have 2 optional parameters (speed and callback)
The speed parameter indicates the milliseconds that it should take effect.
The callback parameter indicates JQUERY function to be executed at the end of the effect.
Example JQUERY fadeOut (speed, callback)
To hide all <p> elements, with a duration of 2 seconds and display an alert that it has completed the effect
2000,
function(){ alert("El parrafo ya no se ve");}
);
9.3.- FadeToggle
The fadeToggle() effect toggles between fadeOut() effect and fadeIn() effect.
That is, if the item is hidden then show it. And conversely, if visible, hidden.
The effect is soft, like magic. At the end of the effect, the result is the same as toggle() effect.
Example JQUERY fadeToggle()
To hide a <p> element that is displayed or to display a hidden element
$("p").fadeToggle();
It also allows to include the parameters "speed" and "callback"
9.4.- FadeTo
The fadeTo() blurring effect allows an element to a degree of opacity.
The opacity is any value between 0 (hidden) and 1 (visible).
It has 2 mandatory parameters (speed, opacy) and 1 optional parameter (callback)
The mandatory parameter speed indicates the milliseconds that it should take effect. Value in milliseconds. Other generic values are "slow" and "fast".
The mandatory parameter opacy indicates the opacity value. Decimal value between 0 and 1. For example 0.15
The optional parameter callback indicates JQUERY function that can be executed at the end of the effect.
Example JQUERY fadeTo()
Slowly diffuse to all <p> elements
Example JQUERY fadeTo()
3 seconds to blend all <p> elements
Example JQUERY fadeTo(speed, callback):
For fast blur all <p> elements and to show an alert that it has completed the effect
10.- JQUERY slide
For the slide effect there are 3 possibilities: slideDown, slideUp and slideToggle
10.1.- slideDown
The effect slideDown() is used to slide down any HTML element that was previously hidden.
Example JQUERY slideDown()
Since the element with the id = "layer" hidden, we do visible by sliding down
You can have 2 optional parameters (speed and callback)
The parameter "speed" indicates the milliseconds that it should take effect.
The parameter "callback" indicates JQUERY function to be executed at the end of the effect.
Example JQUERY slideDown (speed, callback)
To display by sliding the element with id = "layer", with a duration of 2 seconds and display an alert indicating that the effect has ended.
10.2.- slideUp
The effect slideUp() is used to hide by sliding up, any HTML element that is visible.
Example JQUERY slideUp()
Since the element with the id = "layer" visible, we do unseen by sliding up
You can have 2 optional parameters (speed and callback)
The parameter "speed" indicates the milliseconds that it should take effect.
The parameter "callback" indicates JQUERY function to be executed at the end of the effect.
Example JQUERY slideUp (speed, callback)
Eyepiece by sliding the element with id = "layer", with a duration of 2 seconds and display an alert indicating that the effect has ended
2000,
function(){ alert("The effect is complete");}
);
10.3.- slideToggle
The effect slideToggle() is used to switch between sliding up and sliding down.
Example JQUERY slideToggle
If the element with id = "layer" is hidden then we make visible with sliding downwards.
If the element with id = "layer" that is visible then hide it by sliding up.
You can have 2 optional parameters (speed and callback)
The parameter "speed" indicates the milliseconds that it should take effect.
The parameter "callback" indicates JQUERY function to be executed at the end of the effect.
Example JQUERY slideToggle (speed, callback)
To display by sliding the element with id = "layer", with a duration of 2 seconds and display an alert indicating that the effect has ended
2000,
function(){ alert("The effect is complete");}
);
11.- JQUERY animate
The animate() effect lets you create your own animations.
Syntax JQUERY animate()
The params parameter is mandatory and defines the CSS properties to be animated.
The parameter speed is optional and defines the duration of the effect. It can be expressed in milliseconds, or two presets "slow" or "fast".
The parameter callback is optional and allows you to run another function to finish the effect.
Examples JQUERY animate()
We can change the size of text on our <p> to 4em paragraph, slowly to appreciate the change well.
If we want to change the height of a <div> element, we can define a fixed value or an incremental value
$("div").animate({height:'+=80px'});
If we want to move an item <div> a distance of 300 pixels to the right, extend the left margin
We can change several CSS properties at once. It is usual to create a more realistic animation.
The first effect (left) moves the item to the right, because it increases the margin to the left.
The second effect (height) increases the height <div> 80 pixels.
The third effect (width) increases the width <div> 80 pixels.
The fourth effect (opacity) blurs the element to 0.3 (0 is completely hidden).
12.- JQUERY stop
The stop effect serves to stop the animation that is taking effect and leave at that point of the animation.
It can serve for an effect from a minimum value to a maximum value, and the user can choose a value (stop) the duration of the animation.
JQUERY stop
The optional parameter "stopAll" lets stop other subsequent animations. Default is set to "false" value indicates that only the current animation. If then there are more animations, if you execute these.
The optional parameter "gotoEnd" completes the animation immediately, without waiting for the entire duration of the effect. The default value is false, indicating that no full animation, simply makes the point that is. It's like a "pause".
Example JQUERY stop
With a button "start" we can start the effect of sliding down "layer1", which will last 6 seconds element and a "stop" button can stop the effect at the desired point.
13.- JQUERY callback
The parameter callback to define that function executes when an effect has completely finished.
When we run two consecutive effects, you may run the second effect when the previous one has not yet ended.
For example, the alert effect that displays the paragraph "p" is hidden, could be displayed before it is really hidden, because the effect of hiding is performed slowly "slow".
To avoid this, and be sure that when it displays the message "alert" <p> element of concealment is complete, the second effect included in the parameter "callback"
14.- JQUERY chaining
If we have several effects to be applied to the same element, we can apply them in the order in which they appear in the same sentence JQUERY.
For example if we want to apply on <p> element effect change text color to green, sliding up in 2 seconds, and sliding down in 3 seconds.
The effects are applied in the order they appear.
This can be done, if the effects are on the same HTML element.