JQUERY File
JQUERY File
The purpose of jQuery is to make it much easier to use JavaScript on your website.
jQuery takes a lot of common tasks that require many lines of JavaScript code to
accomplish, and wraps them into methods that you can call with a single line of
code.
jQuery also simplifies a lot of the complicated things from JavaScript, like AJAX
calls and DOM manipulation.
HTML/DOM manipulation
CSS manipulation
HTML event methods
Effects and animations
AJAX
Utilities
The jQuery team knows all about cross-browser issues, and they have written
this knowledge into the jQuery library. jQuery will run exactly the same in all
major browsers.
document.getElementById("hello");
$(“#hello”);
document.getElementsByClassName("hello");
$(“.hello”);
document.getElementsByTagName("p");
$(“<p>”);
BASIC SYNTAX
1|Page
$(document).ready(function(){
console.log(“hiii how are you”);
})
</script>
2|Page
jQuery Selectors
jQuery selectors allow you to select and manipulate HTML
element(s).
All selectors in jQuery start with the dollar sign and parentheses:
$().
3|Page
4|Page
What are Events?
All the different visitors' actions that a web page can respond to
are called events.
Examples:
5|Page
jQuery Syntax For Event Methods
$(document).ready()
click()
$("p").click(function(){
$(this).hide();
});
dblclick()
$("p").dblclick(function(){
$(this).hide();
});
mouseenter()
6|Page
The function is executed when the mouse pointer enters the
HTML element:
$("#p1").mouseenter(function(){
alert("You entered p1!");
});
mouseleave()
$("#p1").mouseleave(function(){
alert("Bye! You now leave p1!");
});
mousedown()
$("#p1").mousedown(function(){
alert("Mouse down over p1!");
});
mouseup()
7|Page
$("#p1").mouseup(function(){
alert("Mouse up over p1!");
});
hover()
The first function is executed when the mouse enters the HTML
element, and the second function is executed when the mouse
leaves the HTML element:
$("#p1").hover(function(){
alert("You entered p1!");
},
function(){
alert("Bye! You now leave p1!");
});
focus()
$("input").focus(function(){
$(this).css("background-color", "#cccccc");
});
blur()
$("input").blur(function(){
$(this).css("background-color", "#ffffff");
});
$("p").on("click", function(){
$(this).hide();
});
9|Page
jQuery hide() and show()
With jQuery, you can hide and show HTML elements with
the hide() and show() methods:
$("#hide").click(function(){
$("p").hide();
});
$("#show").click(function(){
$("p").show();
});
Syntax:
$(selector).hide(speed,callback);
$(selector).show(speed,callback);
toggle()
You can also toggle between hiding and showing an element with
the toggle() method.
$("button").click(function(){
$("p").toggle();
});
Syntax:
$(selector).toggle(speed,callback);
10 | P a g e
jQuery Fading Methods
With jQuery you can fade an element in and out of visibility.
fadeIn()
fadeOut()
fadeToggle()
fadeTo()
Syntax:
$(selector).fadeIn(speed,callback);
$("button").click(function(){
$("#div1").fadeIn();
$("#div2").fadeIn("slow");
$("#div3").fadeIn(3000);
});
11 | P a g e
jQuery fadeOut() Method
The jQuery fadeOut() method is used to fade out a visible
element.
Syntax:
$(selector).fadeOut(speed,callback);
$("button").click(function(){
$("#div1").fadeOut();
$("#div2").fadeOut("slow");
$("#div3").fadeOut(3000);
});
If the elements are faded out, fadeToggle() will fade them in.
If the elements are faded in, fadeToggle() will fade them out.
Syntax:
$(selector).fadeToggle(speed,callback);
12 | P a g e
$("button").click(function(){
$("#div1").fadeToggle();
$("#div2").fadeToggle("slow");
$("#div3").fadeToggle(3000);
});
Syntax:
$(selector).fadeTo(speed,opacity,callback);
$("button").click(function(){
$("#div1").fadeTo("slow", 0.15);
$("#div2").fadeTo("slow", 0.4);
$("#div3").fadeTo("slow", 0.7);
});
slideDown()
slideUp()
slideToggle()
Syntax:
$(selector).slideDown(speed,callback);
13 | P a g e
jQuery slideUp() Method
The jQuery slideUp() method is used to slide up an element.
Syntax:
$(selector).slideUp(speed,callback);
If the elements have been slid down, slideToggle() will slide them
up.
If the elements have been slid up, slideToggle() will slide them
down.
$(selector).slideToggle(speed,callback);
The stop() method works for all jQuery effect functions, including
sliding, fading and custom animations.
Syntax:
$(selector).stop(stopAll,goToEnd);
14 | P a g e
The optional goToEnd parameter specifies whether or not to
complete the current animation immediately. Default is false.
Example
$("#stop").click(function(){
$("#panel").stop();
});
Examples
$("button").click(function(){
$("p").hide("slow", function(){
alert("The paragraph is now hidden");
});
});
16 | P a g e
Remove Elements/Content
To remove elements and content, there are mainly two jQuery
methods:
$("#div1").remove();
$("#div1").empty();
17 | P a g e
jQuery Manipulating CSS
jQuery has several methods for CSS manipulation. We will look at
the following methods:
.blue {
color: blue;
}
$(“button”).click(function(){
$(“h1, h2, p”).addClass(“blue”);
$(“div”).addClass(“important”);
});
18 | P a g e
The following example shows how to remove a specific class
attribute from different elements:
$("button").click(function(){
$("h1, h2, p").removeClass("blue");
});
$("button").click(function(){
$("h1, h2, p").toggleClass("blue");
});
Syntax:
$(selector).animate({params},speed,callback);
$("button").click(function(){
$("div").animate({left: '250px'});
});
$("button").click(function(){
$("div").animate({
left: '250px',
opacity: '0.5',
height: '150px',
width: '150px'
});
});
$("button").click(function(){
$("div").animate({
left: '250px',
height: '+=150px',
width: '+=150px'
});
});
20 | P a g e
Animate() - Using Pre-defined
Values
You can even specify a property's animation value as " show",
"hide", or "toggle":
$("button").click(function(){
$("div").animate({
height: 'toggle'
});
});
This means that if you write multiple animate() calls after each
other, jQuery creates an "internal" queue with these method calls.
Then it runs the animate calls ONE by ONE.
$("button").click(function(){
var div = $("div");
div.animate({height: '300px',
opacity: '0.4'}, "slow");
div.animate({width: '300px',
opacity: '0.8'}, "slow");
div.animate({height: '100px',
opacity: '0.4'}, "slow");
div.animate({width: '100px',
21 | P a g e
opacity: '0.8'}, "slow");
});
$("#btn1").click(function(){
alert("Text: " + $("#test").text());
});
$("#btn2").click(function(){
alert("HTML: " + $("#test").html());
});
$("button").click(function(){
alert($("#w3s").attr("href"));
});
22 | P a g e
23 | P a g e
Set Content - text(), html(), and
val()
We will use the same three methods from the previous page
to set content:
$("#btn1").click(function(){
$("#test1").text("Hello world!");
});
$("#btn2").click(function(){
$("#test2").html("<b>Hello world!</b>");
});
$("#btn3").click(function(){
$("#test3").val("Dolly Duck");
});
$("button").click(function(){
$("#w3s").attr("href", "https://www.w3schools.com/jqu
ery/");
});
24 | P a g e
25 | P a g e