SlideShare a Scribd company logo
jQuery basics
What is jQuery?
jQuery is a fast, small, and feature-rich JavaScript library. It makes things
like HTML document traversal and manipulation, event handling,
animation, and Ajax much simpler with an easy-to-use API that works
across a multitude of browsers. With a combination of versatility and
extensibility, jQuery has changed the way that millions of people write
JavaScript.
Who's Using jQuery?
How jQuery Works
jQuery: The Basics
This is a basic tutorial, designed to help you get started using jQuery. If you don't have a test page setup yet, start by creating
the following HTML page:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Demo</title>
</head>
<body>
<a href="http://jquery.com/">jQuery</a>
<script src="jquery.js"></script>
<script>
// Your code goes here.
</script>
</body>
</html>

The src attribute in the <script> element must point to a copy of jQuery. Download a copy of jQuery from
the Downloading jQuery page and store t jquery.js file in the same directory as your HTML file.
Launching Code on Document Ready
Unfortunately, the code doesn't run until all images are finished downloading, including banner ads. To run code as soon as the
document is ready to be manipulated, jQuery has a statement known as the ready event:
1
2
3
4
5

window.onload = function() {
alert( "welcome" );
}

To ensure that their code runs after the browser finishes loading the document, many JavaScript programmers wrap their code in an
onload function:
1
2
3
4
5

$( document ).ready(function() {
// Your code here.
});

For example, inside the ready event, you can add a click handler to the link:
1
2
3
4
5
6
7
8
9

$( document ).ready(function() {
$( "a" ).click(function( event ) {
alert( "Thanks for visiting!" );
});
});
Save your HTML file and reload the test page in your browser. Clicking the link should now first display an alert pop-up, then
continue with the default behavior of navigating to http://jquery.com.
For click and most other events, you can prevent the default behavior by calling event.preventDefault() in the event handler:

1
2
3
4
5
6
7
8
9
1
0
1
1

$( document ).ready(function() {
$( "a" ).click(function( event ) {
alert( "As you can see, the link no longer took you to jquery.com" );
event.preventDefault();
});
});
Adding and Removing an HTML Class
Important: You must place the remaining jQuery examples inside the ready event so that your code executes when the document
is ready to be worked on.
Another common task is adding or removing a class.

First, add some style information into the <head> of the document, like this:
1
2
3
4
5

<style>
a.test {
font-weight: bold;
}
</style>

Next, add the .addClass() call to the script:
1

$( "a" ).addClass( "test" );

All <a> elements are now bold.
To remove an existing class, use .removeClass():
1

$( "a" ).removeClass( "test" );
Special Effects
jQuery also provides some handy effects to help you make your web sites
stand out. For example, if you create a click handler of:

1
2
3
4
5
6
7

$( "a" ).click(function( event ) {
event.preventDefault();
$( this ).hide( "slow" );
});

Then the link slowly disappears when clicked.
Callbacks and Functions
Unlike many other programming languages, JavaScript enables you to
freely pass functions around to be executed at a later time. A callback is a
function that is passed as an argument to another function and is executed
after its parent function has completed. Callbacks are special because they
patiently wait to execute until their parent finishes. Meanwhile, the browser
can be executing other functions or doing all sorts of other work.
To use callbacks, it is important to know how to pass them into their parent
function.
Callback without Arguments
If a callback has no arguments, you can pass it in like this:

1

$.get( "myhtmlpage.html", myCallBack );

When $.get() finishes getting the page myhtmlpage.html, it executes the myCallBack() function.
•Note: The second parameter here is simply the function name (but not as a string, and without parentheses).
Callback with Arguments
Executing callbacks with arguments can be tricky.
Wrong
This code example will not work:
1

$.get( "myhtmlpage.html", myCallBack( param1, param2 ) );

The reason this fails is that the code executes myCallBack( param1, param2 ) immediately and then passes myCallBack()'s return
value as the second parameter to $.get(). We actually want to pass the function myCallBack(), notmyCallBack( param1, param2 )'s
return value (which might or might not be a function). So, how to pass in myCallBack() andinclude its arguments?

Right
To defer executing myCallBack() with its parameters, you can use an anonymous function as a wrapper. Note the use
offunction() {. The anonymous function does exactly one thing: calls myCallBack(), with the values of param1 and param2.
1
2
3
4
5

$.get( "myhtmlpage.html", function() {
myCallBack( param1, param2 );
});

When $.get() finishes getting the page myhtmlpage.html, it executes the anonymous function, which executes
myCallBack( param1, param2 ).
• Thank you ..

More Related Content

PDF
React native app with type script tutorial
PPTX
React outbox
PPTX
Controllers & actions
PPTX
Ajax and ASP.NET AJAX
PPTX
Ajax control tool kit
PDF
Grails Simple Login
PDF
Principles of MVC for PHP Developers
PPTX
React / Redux Architectures
React native app with type script tutorial
React outbox
Controllers & actions
Ajax and ASP.NET AJAX
Ajax control tool kit
Grails Simple Login
Principles of MVC for PHP Developers
React / Redux Architectures

What's hot (20)

PPTX
Using schemas in parsing xml part 2
PPTX
Academy PRO: React JS. Redux & Tooling
PPT
Gems Of Selenium
PDF
My Test Automation Journey
PPTX
Better web apps with React and Redux
PDF
React state managmenet with Redux
PPT
Jsp intro
PDF
Introduction to AngularJS
PDF
MeetJS Summit 2016: React.js enlightenment
PDF
React JS and Redux
PDF
Switch to React.js from AngularJS developer
PDF
React + Redux. Best practices
PPTX
Introduction to angular js for .net developers
PDF
Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014
PDF
1 ppt-ajax with-j_query
PPTX
Javascript void
PDF
Redux vs Alt
PPTX
React + Redux Introduction
PDF
Intro to ReactJS
PPTX
React & redux
Using schemas in parsing xml part 2
Academy PRO: React JS. Redux & Tooling
Gems Of Selenium
My Test Automation Journey
Better web apps with React and Redux
React state managmenet with Redux
Jsp intro
Introduction to AngularJS
MeetJS Summit 2016: React.js enlightenment
React JS and Redux
Switch to React.js from AngularJS developer
React + Redux. Best practices
Introduction to angular js for .net developers
Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014
1 ppt-ajax with-j_query
Javascript void
Redux vs Alt
React + Redux Introduction
Intro to ReactJS
React & redux
Ad

Viewers also liked (9)

PPT
NLADA 2008: Language Access and Technology : Reaching Limited English Profici...
PPTX
Css selectors
PPT
Banosdelmundo
PDF
[Challenge:Future] IDealMaribor
PPT
JQuery Basics
PPT
Introduction To Parallel Computing
PPT
Open Innovation
PPT
Mary Bermeo
PPT
Inteligencia Colectiva
NLADA 2008: Language Access and Technology : Reaching Limited English Profici...
Css selectors
Banosdelmundo
[Challenge:Future] IDealMaribor
JQuery Basics
Introduction To Parallel Computing
Open Innovation
Mary Bermeo
Inteligencia Colectiva
Ad

Similar to jQuery basics (20)

PPTX
PPT
JavaScript Libraries
PPTX
Basics of Java Script (JS)
PPTX
lec 14-15 Jquery_All About J-query_.pptx
PDF
JavaScript: DOM and jQuery
PPTX
jQuery for web development
PDF
react-en.pdf
PPT
The Theory Of The Dom
PDF
Jquery tutorial-beginners
PDF
Web Components v1
PPT
jQuery and_drupal
PPTX
Modern Web Technologies
PPT
eXo SEA - JavaScript Introduction Training
PDF
Asynchronous JavaScript Programming
PPTX
Wt unit 5
PDF
Ajax on drupal the right way - DrupalCamp Campinas, São Paulo, Brazil 2016
PPT
Think jQuery
ODP
Practical catalyst
PPTX
Javascript first-class citizenery
PPTX
Jqueryppt (1)
JavaScript Libraries
Basics of Java Script (JS)
lec 14-15 Jquery_All About J-query_.pptx
JavaScript: DOM and jQuery
jQuery for web development
react-en.pdf
The Theory Of The Dom
Jquery tutorial-beginners
Web Components v1
jQuery and_drupal
Modern Web Technologies
eXo SEA - JavaScript Introduction Training
Asynchronous JavaScript Programming
Wt unit 5
Ajax on drupal the right way - DrupalCamp Campinas, São Paulo, Brazil 2016
Think jQuery
Practical catalyst
Javascript first-class citizenery
Jqueryppt (1)

Recently uploaded (20)

PDF
Complications of Minimal Access Surgery at WLH
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Cell Structure & Organelles in detailed.
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Insiders guide to clinical Medicine.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Cell Types and Its function , kingdom of life
PPTX
GDM (1) (1).pptx small presentation for students
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Complications of Minimal Access Surgery at WLH
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Cell Structure & Organelles in detailed.
human mycosis Human fungal infections are called human mycosis..pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
O5-L3 Freight Transport Ops (International) V1.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Insiders guide to clinical Medicine.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Final Presentation General Medicine 03-08-2024.pptx
Supply Chain Operations Speaking Notes -ICLT Program
Microbial disease of the cardiovascular and lymphatic systems
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Anesthesia in Laparoscopic Surgery in India
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Cell Types and Its function , kingdom of life
GDM (1) (1).pptx small presentation for students
2.FourierTransform-ShortQuestionswithAnswers.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx

jQuery basics

  • 2. What is jQuery? jQuery is a fast, small, and feature-rich JavaScript library. It makes things like HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers. With a combination of versatility and extensibility, jQuery has changed the way that millions of people write JavaScript.
  • 4. How jQuery Works jQuery: The Basics This is a basic tutorial, designed to help you get started using jQuery. If you don't have a test page setup yet, start by creating the following HTML page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <!doctype html> <html> <head> <meta charset="utf-8" /> <title>Demo</title> </head> <body> <a href="http://jquery.com/">jQuery</a> <script src="jquery.js"></script> <script> // Your code goes here. </script> </body> </html> The src attribute in the <script> element must point to a copy of jQuery. Download a copy of jQuery from the Downloading jQuery page and store t jquery.js file in the same directory as your HTML file.
  • 5. Launching Code on Document Ready Unfortunately, the code doesn't run until all images are finished downloading, including banner ads. To run code as soon as the document is ready to be manipulated, jQuery has a statement known as the ready event: 1 2 3 4 5 window.onload = function() { alert( "welcome" ); } To ensure that their code runs after the browser finishes loading the document, many JavaScript programmers wrap their code in an onload function: 1 2 3 4 5 $( document ).ready(function() { // Your code here. }); For example, inside the ready event, you can add a click handler to the link: 1 2 3 4 5 6 7 8 9 $( document ).ready(function() { $( "a" ).click(function( event ) { alert( "Thanks for visiting!" ); }); });
  • 6. Save your HTML file and reload the test page in your browser. Clicking the link should now first display an alert pop-up, then continue with the default behavior of navigating to http://jquery.com. For click and most other events, you can prevent the default behavior by calling event.preventDefault() in the event handler: 1 2 3 4 5 6 7 8 9 1 0 1 1 $( document ).ready(function() { $( "a" ).click(function( event ) { alert( "As you can see, the link no longer took you to jquery.com" ); event.preventDefault(); }); });
  • 7. Adding and Removing an HTML Class Important: You must place the remaining jQuery examples inside the ready event so that your code executes when the document is ready to be worked on. Another common task is adding or removing a class. First, add some style information into the <head> of the document, like this: 1 2 3 4 5 <style> a.test { font-weight: bold; } </style> Next, add the .addClass() call to the script: 1 $( "a" ).addClass( "test" ); All <a> elements are now bold. To remove an existing class, use .removeClass(): 1 $( "a" ).removeClass( "test" );
  • 8. Special Effects jQuery also provides some handy effects to help you make your web sites stand out. For example, if you create a click handler of: 1 2 3 4 5 6 7 $( "a" ).click(function( event ) { event.preventDefault(); $( this ).hide( "slow" ); }); Then the link slowly disappears when clicked.
  • 9. Callbacks and Functions Unlike many other programming languages, JavaScript enables you to freely pass functions around to be executed at a later time. A callback is a function that is passed as an argument to another function and is executed after its parent function has completed. Callbacks are special because they patiently wait to execute until their parent finishes. Meanwhile, the browser can be executing other functions or doing all sorts of other work. To use callbacks, it is important to know how to pass them into their parent function.
  • 10. Callback without Arguments If a callback has no arguments, you can pass it in like this: 1 $.get( "myhtmlpage.html", myCallBack ); When $.get() finishes getting the page myhtmlpage.html, it executes the myCallBack() function. •Note: The second parameter here is simply the function name (but not as a string, and without parentheses).
  • 11. Callback with Arguments Executing callbacks with arguments can be tricky. Wrong This code example will not work: 1 $.get( "myhtmlpage.html", myCallBack( param1, param2 ) ); The reason this fails is that the code executes myCallBack( param1, param2 ) immediately and then passes myCallBack()'s return value as the second parameter to $.get(). We actually want to pass the function myCallBack(), notmyCallBack( param1, param2 )'s return value (which might or might not be a function). So, how to pass in myCallBack() andinclude its arguments? Right To defer executing myCallBack() with its parameters, you can use an anonymous function as a wrapper. Note the use offunction() {. The anonymous function does exactly one thing: calls myCallBack(), with the values of param1 and param2. 1 2 3 4 5 $.get( "myhtmlpage.html", function() { myCallBack( param1, param2 ); }); When $.get() finishes getting the page myhtmlpage.html, it executes the anonymous function, which executes myCallBack( param1, param2 ).
  • 12. • Thank you ..