Jquery Notes Course Tutorial
Jquery Notes Course Tutorial
The purpose of jQuery is to make it much easier to use JavaScript on your website.
What is jQuery?
The purpose of jQuery is to make it much easier to use JavaScript on your website.
jQuery takes a lot of common tasks that requires many lines of JavaScript code to
accomplish, and wraps it 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
Tip: In addition, jQuery has plugins for almost any task out there.
Why jQuery?
There are a lots of other JavaScript frameworks out there, but jQuery seems to be the most
popular, and also the most extendable.
Google
Microsoft
IBM
Netflix
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, including Internet Explorer 6!
jQuery Install
To use jQuery, you need to download the jQuery library (explained below), and include
it on the pages you wish to use it.
The jQuery library is a single JavaScript file, and you reference to it using the HTML
<script> tag:
<head>
<script src="[Link]"></script>
</head>
Notice that the <script> tag should be inside the page's <head> section.
Downloading jQuery
Production version - this is for your live website because it has been minified and
compressed
Development version - this is for testing and development (uncompressed and
readable code)
Alternatives to Downloading
If you don't want to download and host jQuery yourself, you can include it from a CDN
(Content Delivery Network).
Google CDN:
<head>
<script src="[Link]
</script>
</head>
If you look at the Google URL above - the version of jQuery is specified in the
URL (1.8.0). If you would like to use the latest version of jQuery, you can either
remove a number from the end of the version string (for example 1.8), then Google
will return the latest version available in the 1.8 series (1.8.0, 1.8.1, etc.), or you
can take it up to the whole number (1), and Google will return the latest version
available in the 1 series (from 1.1.0 to 1.9.9).
One big advantage of using the hosted jQuery from Google or Microsoft:
Many users already have downloaded jQuery from Google or Microsoft when
visiting another site. As a result, it will be loaded from cache when they visit your
site, which leads to faster loading time. Also, most CDN's will make sure that once
a user requests a file from it, it will be served from the server closest to them,
which also leads to faster loading time.
With jQuery you select (query) HTML elements and perform "actions" on them.
jQuery Syntax
The jQuery syntax is tailor made for selecting HTML elements and perform some action
on the element(s).
Examples:
You might have noticed that all jQuery methods in our examples, are inside a document
ready event:
$(document).ready(function(){
});
It is good practice to wait for the document to be fully loaded and ready, before working
with it. This also allows you to have your JavaScript code before the body of your
document, in the head section.
Here are some examples of actions that can fail if methods are run before the document is
fully loaded:
Tip: The jQuery team has also created an even shorter method for the document ready
event:
$(function(){
});
Use the syntax you prefer. We think that the document ready event is easier to understand
when reading the code.
jQuery Selectors
jQuery selectors are one of the most important parts of the jQuery library.
jQuery Selectors
With jQuery selectors you can find elements based on their id, classes, types, attributes,
values of attributes and much more. It's based on the existing CSS Selectors, and in
addition, it has some own custom selectors.
All type of selectors in jQuery, start with the dollar sign and parentheses: $().
The jQuery element selector selects elements based on their tag names.
$("p")
Example
$(document).ready(function(){
$("button").click(function(){
$("p").hide();
});
});
The jQuery #id selector uses the id attribute of an HTML tag to find the specific element.
An id should be unique within a page, so you should use the #id selector when you want
to find a single, unique element.
To find an element with a specific id, write a hash character, followed by the id of the
element:
$("#test")
Example
When a user clicks on a button, the element with id="test" will be hidden:
Example
$(document).ready(function(){
$("button").click(function(){
$("#test").hide();
});
});
To find elements with a specific class, write a period character, followed by the name of
the class:
$(".test")
Example
When a user clicks on a button, the elements with class="test" will be hidden:
Example
$(document).ready(function(){
$("button").click(function(){
$(".test").hide();
});
});
Syntax Description
$("*") Selects all elements
$(this) Selects the current HTML element
$("[Link]") Selects all <p> elements with class="intro"
$("p:first") Selects the first <p> element
$("ul li:first") Selects the first <li> element of the first <ul>
$("ul li:first-child") Selects the first <li> element of every <ul>
$("[href]") Selects all elements with an href attribute
Selects all <a> elements with a target attribute value
$("a[target='_blank']") equal to "_blank"
Selects all <a> elements with a target attribute value
$("a[target!='_blank']") NOT equal to "_blank"
Selects all <button> elements and <input> elements of
$(":button") type="button"
$("tr:even") Selects all even <tr> elements
$("tr:odd") Selects all odd <tr> elements
Event handlers are methods that are called when "something happens" in HTML.
It is common to put jQuery code into event handler methods in the <head> section.
In the example below, a function is called when the click event for the button is
triggered:
Example
<!DOCTYPE html>
<html>
<head>
<script src="[Link]"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").hide();
});
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Click me</button>
</body>
</html>
If your website contains a lot of pages, and you want your jQuery functions to be easy to
maintain, you can put your jQuery functions in a separate .js file.
When we demonstrate jQuery in this tutorial, the functions are added directly into the
<head> section. However, sometimes it is preferable to place them in a separate file,
like this (use the src attribute to refer to the .js file):
Example
<head>
<script src="[Link]"></script>
<script src="my_jquery_functions.js"></script>
</head>
With jQuery, you can hide and show HTML elements with the hide() and show()
methods:
Example
$("#hide").click(function(){
$("p").hide();
});
$("#show").click(function(){
$("p").show();
});
Syntax:
$(selector).hide(speed,callback);
$(selector).show(speed,callback);
The optional speed parameter specifies the speed of the hiding/showing, and can take
the following values: "slow", "fast", or milliseconds.
The optional callback parameter is the name of a function to be executed after hide (or
show) completes.
Example
$("button").click(function(){
$("p").hide(1000);
});
jQuery toggle()
With jQuery, you can toggle between the hide() and show() methods with the toggle()
method.
Example
$("button").click(function(){
$("p").toggle();
});
Syntax:
$(selector).toggle(speed,callback);
The optional speed parameter can take the following values: "slow", "fast", or
milliseconds.
The optional callback parameter is the name of a function to be executed after the
toggle() method completes.
fadeIn()
fadeOut()
fadeToggle()
fadeTo()
Syntax:
$(selector).fadeIn(speed,callback);
The optional speed parameter specifies the duration of the effect. It can take the
following values: "slow", "fast", or milliseconds.
The optional callback parameter is the name of a function to be executed after the fading
completes.
The following example demonstrates the fadeIn() method with different parameters:
Example
$("button").click(function(){
$("#div1").fadeIn();
$("#div2").fadeIn("slow");
$("#div3").fadeIn(3000);
});
Syntax:
$(selector).fadeOut(speed,callback);
The optional speed parameter specifies the duration of the effect. It can take the
following values: "slow", "fast", or milliseconds.
The optional callback parameter is the name of a function to be executed after the fading
completes.
The following example demonstrates the fadeOut() method with different parameters:
Example
$("button").click(function(){
$("#div1").fadeOut();
The jQuery fadeToggle() method toggles between the fadeIn() and fadeOut() methods.
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);
The optional speed parameter specifies the duration of the effect. It can take the
following values: "slow", "fast", or milliseconds.
The optional callback parameter is the name of a function to be executed after the fading
completes.
Example
$("button").click(function(){
$("#div1").fadeToggle();
$("#div2").fadeToggle("slow");
$("#div3").fadeToggle(3000);
});
The jQuery fadeTo() method allows fading to a given opacity (value between 0 and 1).
Syntax:
$(selector).fadeTo(speed,opacity,callback);
The required opacity parameter in the fadeTo() method specifies fading to a given
opacity (value between 0 and 1).
The optional callback parameter is the name of a function to be executed after the function
completes.
The following example demonstrates the fadeTo() method with different parameters:
Example
$("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);
The optional speed parameter specifies the duration of the effect. It can take the
following values: "slow", "fast", or milliseconds.
The optional callback parameter is the name of a function to be executed after the
sliding completes.
Example
$("#flip").click(function(){
$("#panel").slideDown();
});
Syntax:
$(selector).slideUp(speed,callback);
The optional speed parameter specifies the duration of the effect. It can take the
following values: "slow", "fast", or milliseconds.
The optional callback parameter is the name of a function to be executed after the
sliding completes.
Example
$("#flip").click(function(){
$("#panel").slideUp();
});
The jQuery slideToggle() method toggles between the slideDown() and slideUp()
methods.
If the elements are slide down, slideToggle() will slide them up.
If the elements are slide up, slideToggle() will slide them down.
$(selector).slideToggle(speed,callback);
The optional speed parameter can take the following values: "slow", "fast", milliseconds.
The optional callback parameter is the name of a function to be executed after the
sliding completes.
Example
$("#flip").click(function(){
$("#panel").slideToggle();
});
For a complete overview of all jQuery effects, please go to our jQuery Effect Reference.
Syntax:
The optional speed parameter specifies the duration of the effect. It can take the
following values: "slow", "fast", or milliseconds.
The optional callback parameter is the name of a function to be executed after the
animation completes.
The following example demonstrates a simple use of the animate() method; it moves a
<div> element to the left, until it has reached a left property of 250px:
Example
$("button").click(function(){
$("div").animate({left:'250px'});
});
By default, all HTML elements have a static position, and cannot be moved.
To manipulate the position, remember to first set the CSS position property of the
element to relative, fixed, or absolute!
Example
$("button").click(function(){
$
("div").animate({ left:'250p
x',
opacity:'0.5',
height:'150px',
width:'150px'
});
});
It is also possible to define relative values (the value is then relative to the element's
current value). This is done by putting += or -= in front of the value:
Example
$("button").click(function(){
$
("div").animate({ left:'250p
x', height:'+=150px',
width:'+=150px'
});
});
You can even specify a property's animation value as "show", "hide", or "toggle":
Example
$("button").click(function(){
$
("div").animate({ height:'to
ggle'
});
});
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.
So, if you want to perform different animations after each other, we take advantage of
the queue functionality:
Example 1
$("button").click(function(){
var div=$("div");
[Link]({height:'300px',opacity:'0.4'},"slow");
[Link]({width:'300px',opacity:'0.8'},"slow");
[Link]({height:'100px',opacity:'0.4'},"slow");
[Link]({width:'100px',opacity:'0.8'},"slow");
});
The example below first moves the <div> element to the right, and then increases the
font size of the text:
Example 2
$("button").click(function(){
var div=$("div");
[Link]({left:'100px'},"slow");
[Link]({fontSize:'3em'},"slow");
});
The jQuery stop() method is used to stop animations or effects before it is finished.
The jQuery stop() method is used to stop an animation or effect before it is finished.
The stop() method works for all jQuery effect functions, including sliding, fading and
custom animations.
Syntax:
$(selector).stop(stopAll,goToEnd);
The optional stopAll parameter specifies whether also the animation queue should be
cleared or not. Default is false, which means that only the active animation will be
JQuery Notes pg 19 of43
stopped, allowing any queued animations to be performed afterwards.
The optional goToEnd parameter specifies whether or not to complete the current
animation immediately. Default is false.
So, by default, the stop() method kills the current animation being performed on the
selected element.
Example
$("#stop").click(function(){
$("#panel").stop();
});
JavaScript statements are executed line by line. However, with effects, the next line of
code can be run even though the effect is not finished. This can create errors.
Examples
The example below has a callback parameter that is a function that will be executed
after the hide effect is completed:
$("button").click(function(){
$("p").hide("slow",function(){
alert("The paragraph is now hidden");
});
});
The example below has no callback parameter, and the alert box will be displayed
before the hide effect is completed:
$("button").click(function(){
$("p").hide(1000);
alert("The paragraph is now hidden");
});
jQuery - Chaining
Chaining allows us to run multiple jQuery methods (on the same element) within a
single statement.
Until now we have been writing jQuery statements one at a time (one after the other).
Tip: This way, browsers do not have to find the same element(s) more than once.
To chain an action, you simply append the action to the previous action.
The following example chains together the css(), slideUp(), and slideDown() methods.
The "p1" element first changes to red, then it slides up, and then it slides down:
Example
$("#p1").css("color","red").slideUp(2000).slideDown(2000);
Tip: When chaining, the line of code could become quite long. However, jQuery is not
very strict on the syntax; you can format it like you want, including line breaks and
indentations.
Example
$("#p1").css("color","red")
.slideUp(2000)
.slideDown(2000);
jQuery contains powerful methods for changing and manipulating HTML elements and
attributes.
One very important part of jQuery, is the possibility to manipulate the DOM.
jQuery comes with a bunch of DOM related methods, that makes it easy to access and
manipulate elements and attributes.
The DOM defines a standard for accessing HTML and XML documents:
Three simple, but useful, jQuery methods for DOM manipulation is:
The following example demonstrates how to get content with the jQuery text() and
html() methods:
Example
$("#btn1").click(function(){
alert("Text: " + $("#test").text());
});
$("#btn2").click(function(){
alert("HTML: " + $("#test").html());
});
The following example demonstrates how to get the value of an input field with the
jQuery val() method:
Example
$("#btn1").click(function(){
alert("Value: " + $("#test").val());
});
The following example demonstrates how to get the value of the href attribute in a link:
Example
$("button").click(function(){
alert($("#w3s").attr("href"));
});
We will use the same three methods from the previous page to set content:
The following example demonstrates how to set content with the jQuery text(), html(),
and val() methods:
Example
$("#btn1").click(function(){
$("#test1").text("Hello world!");
});
$("#btn2").click(function(){
$("#test2").html("<b>Hello world!</b>");
});
$("#btn3").click(function(){
$("#test3").val("Dolly Duck");
});
The following example demonstrates text() and html() with a callback function:
Example
$("#btn1").click(function(){
$("#test1").text(function(i,origText){
return "Old text: " + origText + " New text: Hello world!
(index: " + i + ")";
});
});
$("#btn2").click(function(){
$("#test2").html(function(i,origText){
return "Old html: " + origText + " New html: Hello <b>world!</b>
(index: " + i + ")";
});
});
The following example demonstrates how to change (set) the value of the href attribute
in a link:
Example
$("button").click(function(){
$("#w3s").attr("href","[Link]
});
The attr() method also allows you to set multiple attributes at the same time.
The following example demonstrates how to set both the href and title attributes at the
same time:
$("button").click(function(){
$("#w3s").attr({
"href" : "[Link]
"title" : "W3Schools jQuery Tutorial"
});
});
The jQuery method attr(), also come with a callback function. The callback function has
two parameters: the index of the current element in the list of elements selected and the
original (old) attribute value. You then return the string you wish to use as the new
attribute value from the function.
Example
$("button").click(function(){
$("#w3s").attr("href", function(i,origValue)
{ return origValue + "/jquery";
});
});
We will look at four jQuery methods that is used to add new content:
The jQuery append() method inserts content AT THE END of the selected HTML
elements.
Example
The jQuery prepend() method inserts content AT THE BEGINNING of the selected
HTML elements.
Example
In both examples above, we have only inserted some text/HTML at the beginning/end of
the selected HTML elements.
However, both the append() and prepend() methods can take an infinite number of new
elements as parameters. The new elements can be generated with text/HTML (like we
have done in the examples above), with jQuery, or with JavaScript code and DOM
elements.
In the following example, we create several new elements. The elements are created with
text/HTML, jQuery, and JavaScript/DOM. Then we append the new elements to the text
with the append() method (this would have worked for prepend() too) :
Example
The jQuery after() method inserts content AFTER the selected HTML elements.
The jQuery before() method inserts content BEFORE the selected HTML elements.
Example
Also, both the after () and before () methods can take an infinite number of new
elements as parameters. The new elements can be generated with text/HTML (like we
have done in the example above), with jQuery, or with JavaScript code and DOM
elements.
In the following example, we create several new elements. The elements are created
with text/HTML, jQuery, and JavaScript/DOM. Then we insert the new elements to the
text with the after() method (this would have worked for before() too) :
Example
function afterText()
{
var txt1="<b>I </b>"; // Create element with HTML
var txt2=$("<i></i>").text("love "); // Create with jQuery
var txt3=[Link]("big"); // Create with DOM
[Link]="jQuery!";
Remove Elements/Content
To remove elements and content, there are mainly two jQuery methods:
The jQuery remove() method removes the selected element(s) and its child elements.
Example
$("#div1").remove();
The jQuery empty() method removes the child elements of the selected element(s).
Example
$("#div1").empty();
The jQuery remove() method also accepts one parameter, which allows you to filter the
elements to be removed.
Example
$("p").remove(".italic");
jQuery has several methods for CSS manipulation. We will look at the following
methods:
Example Stylesheet
The following stylesheet will be used for all the examples on this page:
.important
{
font-weight:bold;
font-size:xx-large;
}
.blue
{
color:blue;
}
The following example shows how to add class attributes to different elements. Of
course you can select multiple elements, when adding classes:
Example
$("button").click(function(){
$("h1,h2,p").addClass("blue");
$("div").addClass("important");
});
You can also specify multiple classes within the addClass() method:
Example
$("button").click(function(){
$("#div1").addClass("important blue");
});
The following example shows how to remove a specific class attribute from different
elements:
Example
$("button").click(function(){
$("h1,h2,p").removeClass("blue");
});
Example
$("button").click(function(){
$("h1,h2,p").toggleClass("blue");
});
The css() method sets or returns one or more style properties for the selected elements.
To return the value of a specified CSS property, use the following syntax:
css("propertyname");
The following example will return the background-color value of the FIRST matched
element:
Example
$("p").css("background-color");
css("propertyname","value");
The following example will set the background-color value for ALL matched elements:
Example
$("p").css("background-color","yellow");
css({"propertyname":"value","propertyname":"value",...});
JQuery Notes pg 32 of43
The following example will set a background-color and a font-size for ALL matched
elements:
Example
$("p").css({"background-color":"yellow","font-size":"200%"});
AJAX is the art of exchanging data with a server, and updating parts of a web page -
without reloading the whole page.
What is AJAX?
Examples of applications using AJAX: Gmail, Google Maps, Youtube, and Facebook tabs.
With the jQuery AJAX methods, you can request text, HTML, XML, or JSON from a
remote server using both HTTP Get and HTTP Post - And you can load the external
data directly into the selected HTML elements of your web page!
Writing regular AJAX code can be a bit tricky, because different browsers have
different syntax for AJAX implementation. This means that you will have to write
extra code to test for different browsers. However, the jQuery team has taken care of
this for us, so that we can write AJAX functionality with only one single line of code.
In the next chapters we will look at the most important jQuery AJAX methods.
The load() method loads data from a server and puts the returned data into the selected
element.
Syntax:
The required URL parameter specifies the URL you wish to load.
The optional data parameter specifies a set of querystring key/value pairs to send along
with the request.
The optional callback parameter is the name of a function to be executed after the load()
method is completed.
The following example loads the content of the file "demo_test.txt" into a specific <div>
element:
Example
$("#div1").load("demo_test.txt");
The following example loads the content of the element with id="p1", inside the file
"demo_test.txt", into a specific <div> element:
Example
$("#div1").load("demo_test.txt #p1");
The optional callback parameter specifies a callback function to run when the load()
method is completed. The callback function can have different parameters:
The following example displays an alert box after the load method() completes. If the
load() method has succeed, it displays "External content loaded successfully!", and if it
fails it displays an error message:
Example
$("button").click(function(){
$("#div1").load("demo_test.txt",function(responseTxt,statusTxt,xhr)
{ if(statusTxt=="success")
alert("External content loaded successfully!");
if(statusTxt=="error")
alert("Error: "+[Link]+": "+[Link]);
JQuery Notes pg 35 of43
});
});
The jQuery get() and post() methods is used to request data from the server with an
HTTP GET or POST request.
Two commonly used methods for a request-response between a client and server are:
GET and POST.
GET is basically used for just getting (retrieving) some data from the server. Note: The
GET method may return cached data.
POST can also be used to get some data from the server. However, the POST method
NEVER caches data, and is often used to send data along with the request.
To learn more about GET and POST, and the differences between the two methods,
please read our HTTP Methods GET vs POST chapter.
The $.get() method requests data from the server with an HTTP GET request.
Syntax:
$.get(URL,callback);
The required URL parameter specifies the URL you wish to request.
The optional callback parameter is the name of a function to be executed if the request
succeeds.
The following example uses the $.get() method to retrieve data from a file on the server:
$("button").click(function(){
$.get("demo_test.asp",function(data,status){ alert("Data:
" + data + "\nStatus: " + status);
});
});
The second parameter is a callback function. The first callback parameter holds the
content of the page requested, and the second callback parameter holds the status of the
request.
<%
[Link]("This is some text from an external ASP file.")
%>
The $.post() method requests data from the server using an HTTP POST request.
Syntax:
$.post(URL,data,callback);
The required URL parameter specifies the URL you wish to request.
The optional data parameter specifies some data to send along with the request.
The optional callback parameter is the name of a function to be executed if the request
succeeds.
Example
$("button").click(function(){
$.post("demo_test_post.asp",
{
name:"Donald
Duck",
city:"Duckburg"
},
function(data,status){
alert("Data: " + data + "\nStatus: " + status);
});
});
Then we pass in some data to send along with the request (name and city).
The ASP script in "demo_test_post.asp" reads the parameters, process them, and return a
result.
The third parameter is a callback function. The first callback parameter holds the
content of the page requested, and the second callback parameter holds the status of the
request.
<%
dim fname,city
fname=[Link]("name")
city=[Link]("city")
[Link]("Dear " & fname & ".
")
[Link]("Hope you live well in " & city & ".")
%>
What if you wish to use other frameworks on your pages, while still using jQuery?
As you already know; jQuery uses the $ sign as a shortcut for jQuery.
Some of the other frameworks also use the $ character as a shortcut (just like jQuery),
and then you suddenly have two different frameworks using the same shortcut, which
might result in that your scripts stop working.
The jQuery team have already thought about this, and implemented the noConflict()
method.
The noConflict() method releases the hold on the $ shortcut identifier, so that other
scripts can use it.
You can of course still use jQuery, simply by writing the full name instead of the
shortcut:
Example
$.noConflict();
jQuery(document).ready(function()
{ jQuery("button").click(function(){
jQuery("p").text("jQuery is still working!");
});
});
You can also create your own shortcut very easily. The noConflict() method returns a
reference to jQuery, that you can save in a variable, for later use. Here is an example:
Example
var jq = $.noConflict();
jq(document).ready(function()
{ jq("button").click(function()
{
jq("p").text("jQuery is still working!");
});
});
Example
$.noConflict();
jQuery(document).ready(function($){
$("button").click(function(){
$("p").text("jQuery is still working!");
});
});
Key Concepts:
Selectors:
jQuery uses selectors (similar to CSS selectors) to target HTML elements.
$("p") selects all <p> elements.
$("#myId") selects the element with id="myId".
$(".myClass") selects all elements with class="myClass".
Actions:
jQuery allows you to perform actions on selected elements.
.hide() hides the selected elements.
.show() shows the selected elements.
.css() manipulates the CSS styles.
.click() handles click events.
Syntax:
The basic syntax is $(selector).action(). The $ sign is used to access jQuery.
DOM Manipulation:
jQuery simplifies the process of modifying the structure and content of HTML
documents.
Event Handling:
jQuery provides methods for handling user interactions like clicks, mouseovers, and form
submissions.
JQuery Notes pg 41 of43
Effects and Animations:
jQuery makes it easy to create animations and visual effects like fading, sliding, and
custom animations.
AJAX:
jQuery simplifies asynchronous communication between the client and server.
How to Include jQuery:
There are two ways to include jQuery in a web page:
Local Installation:
Download the jQuery library from the official website and include it in your HTML code
using a <script> tag.
CDN:
Link to the jQuery library from a Content Delivery Network (CDN) using a <script> tag.
Example:
Code
<!DOCTYPE html>
<html>
<head>
<title>jQuery Example</title>
<script src="[Link]
</head>
<body>
<p>This is a paragraph.</p>
<button>Hide Paragraph</button>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").hide();
});
});
</script>
</body>
</html>
Explanation:
The code includes jQuery via CDN.
$(document).ready(function(){ ... }); ensures the code runs after the DOM is fully loaded.
$("button").click(function(){ ... }); sets up a click event handler for the button.
$("p").hide(); hides all paragraph elements when the button is clicked.
Basic Functions
Hide/Show: .hide(), .show(), .toggle().
Fade: .fadeIn(), .fadeOut(), .fadeToggle().