durgasoft_jQuery
durgasoft_jQuery
TECHNOLOGIES
Study Material
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
140 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
UI
TECHNOLOGIES
jQuery
Official website: jquery.com
The main advantage of jquery is it provides several methods and objects in the form of
javascript file, so that we can use these directly and developer's life got simplified.
Note: In Plain old java script(also known as Vanilla Java Script),we have to write every
thing manually. But if we jQuery,we are not required to write much code and we can use
its library directly.
1) var myh1=document.querySelectorAll('h1')
2) for( h1 of myh1){
3) h1.style.color='red';
4) }
jQuery Code:
$('h1').css('color','red')
Advantages of jQuery:
1) It provide several built in methods and objects. We can use these directly so that
development will become very easy.
2) Clear and Shorter code
3) Ease of use
4) Cross Browser support
5) AJAX support
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
141 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
UI
TECHNOLOGIES
Limitations of jQuery:
1) What ever jQuery doing, we can implement everything without jQuery also. ie jQuery
won't do any things extra.
2) The total library should be loaded compulsory, which creates performance problems.
Note: Because of above limitations, some part of the developer's community won't
recommend jQuery usage.
Eg: youmightnotneedjquery.com
1) By Locally:
Download jQuery.js file from jquery.com https://code.jquery.com/jquery-3.3.1.js
Download and place in application folder.
Inside head of html we have to write <script> tag as follows
<script type="text/javascript" src="jquery.js"></script>
2) By using CDN:
www.code.jquery.com
1) <script
2) src="http://code.jquery.com/jquery-3.3.1.js"
3) integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
4) crossorigin="anonymous"></script>
jQuery Selectors:
In Vanilla Javascript we have several methods to select/grab html elements like
getElementById()
getElementsByClassName()
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
142 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
UI
TECHNOLOGIES
getElementsByTagName()
querySelector()
querySelectorAll()
etc
But in jQuery we have only one way to select html elements.i.e to use $ symbol
Demo Application
demo.html
1) <!DOCTYPE html>
2) <html lang="en" dir="ltr">
3) <head>
4) <script
5) src="http://code.jquery.com/jquery-3.3.1.js"
6) integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
7) crossorigin="anonymous"></script>
8) <meta charset="utf-8">
9) <title></title>
10) </head>
11) <body>
12) <h1>Hello First H1</h1>
13) <h2>Favourate Drink:</h2>
14) <ul>
15) <li>KingFisher</li>
16) <li>KnockOut</li>
17) <li>Foster</li>
18) <li id='special'>HumanBlood</li>
19) </ul>
20) <a href="https://google.com">Click Here to go to Google</a>
21) </body>
22) </html>
jQuery code:
$('h1')
$('li')
$('#special')
$('body')
etc...
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
143 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
UI
TECHNOLOGIES
Syntax: $(selector).css(property,value)
Examples:
$('h1').css('color','white');
$('h1').css('background','red');
$('h1').css('border','5px solid green');
var x = $('h1')
x.css('color','white');
x.css('background','red');
x.css('border','5px solid green');
Instead of passing parameters one by one,we can create Object and pass that object
directly.
1) var x = $('h1')
2) var myCSS={
3) color:'white',
4) background:'green',
5) border:'red 5px solid'
6) }
7) x.css(myCSS);
Note: We can use $ Symbol to select and css() Method to manipulate HTML Elements.
$('element:first')
$('element:last')
$('element:first-of-type')
$('element:nth-of-type(n)')
etc...
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
144 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
UI
TECHNOLOGIES
Eg:
1) $('h1') Selects all h1 tags
2) $('h1:first') Selects only first h1 tag
3) $('h1').first() Selects only first h1 tag
4) $('h1:first-of-type') Selects only first h1 tag
5) $('h1:nth-of-type(2)') Selects second h1 tag
6) $('h1:last') Selects only last h1 tag
7) $('h1').last() Selects only last h1 tag
Q1) Write Vanilla JavaScript and jQuery Codes to Change all h1 Tags
Text Color as White and Background as Red
Vanilla JavaScript Code:
1) var allh1s=document.querySelectorAll('h1');
2) for(h1 of allh1s){
3) h1.style.color='white';
4) h1.style.background='red';
5) }
jQuery Code:
1) var mystyles={
2) color:'white',
3) background:'red'
4) };
5) $('h1').css(mystyles)
6)
7) Instead of this we can write directly as follows
8)
9) $('h1').css({
10) color:'white',
11) background:'red'
12) });
1) var x=document.querySelectorAll('li');
2) for(li of x){
3) li.style.fontSize='20px';
4) }
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
145 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
UI
TECHNOLOGIES
jQuery Code:
$('li').css('fontSize','20px')
Note: The biggest advantage of jQuery is we can do more things with less Code (Write
less, do More)
Demo Application
demo.html
1) <!DOCTYPE html>
2) <html lang="en" dir="ltr">
3) <head>
4) <script
5) src="http://code.jquery.com/jquery-2.2.4.js"
6) integrity="sha256-iT6Q9iMJYuQiMWNd9lDyBUStIq/8PuOW33aOqmvFpqI="
7) crossorigin="anonymous"></script>
8) <meta charset="utf-8">
9) <title></title>
10) </head>
11) <body>
12) <p>This is First Paragraph</p>
13) <p id="second">This is Second Paragraph</p>
14) <p class="remaining">This is Third Paragraph</p>
15) <p class="remaining">This is Fourth Paragraph</p>
16) </body>
17) </html>
Case-2: Select all <p> tags with class 'remaining' and make them 200px wide(width)
$('.remaining').css('width','200px')
Case-3: Select all <p> tags with id 'second' and give red solid 10px border.
$('#second').css('border','10px solid red')
case-4: Select only first <p> tag and change text color as white
$('p:first').css('color','white')
case-4: Select only third <p> tag and change font-size as 30px
$('p:nth-of-type(3)').css('fontSize','30px')
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
146 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
UI
TECHNOLOGIES
1) text():
We can use this method to get and set text of the matched elements. i.e this method
acts as both getter and setter method.
text() To get text of all matched elements including child tags.In this case this
method acts as getter method
text(content) To set provided text content for every matched element. In this case
this method acts as setter method.
Note: If any method developed to acts as both getter and setter method then it is said to
be this method follows getter and setter paradigm.
1) <!DOCTYPE html>
2) <html lang="en" dir="ltr">
3) <head>
4) <script
5) src="http://code.jquery.com/jquery-2.2.4.js"
6) integrity="sha256-iT6Q9iMJYuQiMWNd9lDyBUStIq/8PuOW33aOqmvFpqI="
7) crossorigin="anonymous"></script>
8) <meta charset="utf-8">
9) <title></title>
10) </head>
11) <body>
12) <h1>This is H1 Data</h1>
13) <h2>Choose Your Favourate Subject:</h2>
14) <ul>
15) <li>HTML</li>
16) <li>JavaScript</li>
17) <li>CSS</li>
18) <li>jQuery</li>
19) </ul>
20) </body>
21) </html>
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
147 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
UI
TECHNOLOGIES
2) html():
We can use this method to get and set html content.
If we are not passing any argument then this method acts as getter method, which
returns HTML contents of the first matched element.
Eg: $('h1').html()
If we are passing argument then this method acts as setter method,which sets the
HTML contents of every matched element.
Eg: $('li').html('<a href="https://amazon.com">AMAZON</a>')
3) attr():
We can use this method to get and set attribute values.
attr(attributename) To get the value of specified attribute of the first matched
element.
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
148 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
UI
TECHNOLOGIES
4) val() Method:
We can use val() method to get value of the first matched element.
Eg: value entered in the text box
which radio button selected
which value selected from dropdown box
We can also use val() method to set value for every matched element.
val() Getter Method
val(text) Setter Method
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
149 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
UI
TECHNOLOGIES
Usecase: While implementing reset button functionality to clear all input fields this
method is helpful.
7) toggleClass(): We can use this method to add and remove classes from the
matched elements.
If the specified class already set then it will be removed. It is not already set then the class
will be added for every matched element.
1) <!DOCTYPE html>
2) <html lang="en" dir="ltr">
3) <head>
4) <script
5) src="http://code.jquery.com/jquery-2.2.4.js"
6) integrity="sha256-iT6Q9iMJYuQiMWNd9lDyBUStIq/8PuOW33aOqmvFpqI="
7) crossorigin="anonymous"></script>
8) <meta charset="utf-8">
9) <title></title>
10) <style >
11) .high{
12) color:white;
13) background:red;
14) }
15) .low{
16) color:white;
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
150 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
UI
TECHNOLOGIES
1) $('li').addClass('high')
It will add 'high' class to every li tag
2) $('li:nth-of-type(even)').removeClass('high')
It will remove 'high' class from every even numbered li tag.
3) $('li:nth-of-type(2)').addClass('completed')
It will add 'completed' class only for 2nd li
4) $('li').toggleClass('low')
It will add 'low' class for every li tag(because this class is not already set)
5) $('li').toggleClass('low')
It will remove 'low' class from every li tag(because this class is already set)
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
151 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
UI
TECHNOLOGIES
1) click():
jQuery click() method can be used to add a click listener to the elements.
1) $('h1').click(function(){
2) alert('h1 tag got clicked');
3) });
Eg 2: To raise alert message and to change background color of last button on click event
1) $('button:last').click(function(){
2) alert('Hello Stupid Dont Sleep I will Kill You!!!');
3) $(this).css('background','red')
4) });
Note: In Vanilla Java Script, 'this' always represent current element. But in jQuery we
have to use $(this)
Demo Application:
1) <!DOCTYPE html>
2) <html lang="en" dir="ltr">
3) <head>
4) <script
5) src="http://code.jquery.com/jquery-2.2.4.js"
6) integrity="sha256-iT6Q9iMJYuQiMWNd9lDyBUStIq/8PuOW33aOqmvFpqI="
7) crossorigin="anonymous"></script>
8) <meta charset="utf-8">
9) <title></title>
10) </head>
11) <body>
12) <h1>jQuery Event Handler Demo </h1>
13) <button type="button" name="button">Dont Sleep First Warning</button>
14) <button type="button" name="button">Dont Sleep Second Warning</button>
15) <button type="button" name="button">Dont Sleep Third Warning</button>
16) <script type="text/javascript" src="demo.js">
17) </script>
18) </body>
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
152 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
UI
TECHNOLOGIES
19) </html>
demo.js:
1) $('h1').click(function(){
2) alert('h1 tag clicked')
3) })
4) $('button:first').click(function(){
5) alert('Hello Dont Sleep');
6) $(this).css('background','yellow')
7) });
8) $('button:nth-of-type(2)').click(function(){
9) alert('Dont Sleep I will beat you');
10) $(this).css('background','orange')
11) });
12) $('button:last').click(function(){
13) alert('Hello Stupid Dont Sleep I will Kill You!!!');
14) $(this).css('background','red')
15) });
2) keypress():
We can use this method to add keypress listener to elements.
i.e whenever we are pressing the key if we want to do any activity automatically then
we should go for keypress() method.
Eg: To raise alert message for every character typed in text box
Enter Name: <input type='text'>
jQuery Code:
1) $('input').keypress(function(){
2) alert('Inserted one character!!!!');
3) })
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
153 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
UI
TECHNOLOGIES
Note: The key-code for 'x' is: 120 where as for 'X' is: 88
3) on():
on() is the most commonly used method to perform event handling in jQuery.
It is similar to Vanilla Java Script addEventListener() method.
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
154 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
UI
TECHNOLOGIES
Whenever mouseout event happend, the text of h1 tag should be changed to 'Hyderabad
City' with green background and white text.
1) $('h1').on('mouseover',function(){
2) $(this).text('BANGALORE CITY')
3) $(this).css({background:'red',color:'white'});
4) })
5) $('h1').on('mouseout',function(){
6) $(this).text('HYDERABAD CITY')
7) $(this).css({background:'green',color:'white'});
8) })
Eg 2: For button
Single Click: alert message as Hello Stupid dont click
Double Click: alert message as Hello Animal I will kill you
html
jQuery
1) $('button:first').on('click',function() {
2) alert('Hello Stupid Dont Click!!!')
3) })
4)
5) $('button:last').on('dblclick',function() {
6) alert('Hello Animal I Will Kill You!!!')
7) })
jQuery Effects:
jQuery provides several in-built effects.But main important effects are:
1) Fading Effects
2) Sliding Effects
https://api.jquery.com/category/effects/
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
155 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
UI
TECHNOLOGIES
I) Fading Effects:
jQuery defines the following methods for fading purposes
1) fadeOut():
Hide the matched elements by fading them to transparent.
2) fadeIn():
Display the matched elements which are fadeout
3) fadeToggle():
Display or hide the matched elements
If already fadeOut then fadeIn will be performed.
If already fadeIn then fadeOut will be performed.
Demo Application
demo.html
1) <!DOCTYPE html>
2) <html lang="en" dir="ltr">
3) <head>
4) <style>
5) div{
6) height: 100px;
7) width: 100px;
8) background: red;
9) color:white;
10) margin:20px;
11) text-align: center;
12) float: left;
13) }
14) </style>
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
156 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
UI
TECHNOLOGIES
demo.js
1) $('button').on('click',function(){
2) $('div').fadeToggle(2000);
3) })
Note: Whenever we perform fadeout just elements will be hidden but won't be removed.
But based on requirement we can remove the matched elements also.
1) $('button').on('click',function(){
2) $('div').fadeToggle(2000,function(){
3) $(this).remove()}
4) );
5) })
1) slideUp():
Hide the matched elements with a sliding motion.
2) slideDown():
Display the matched elements with a sliding motion.
3) slideToggle()
Display or hide the matched elements with a sliding motion.
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
157 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
UI
TECHNOLOGIES
Eg:
$('button').on('click',function(){
$('div').slideToggle(2000);
})
Note: passing function as argument, remove matched elements are exactly same as
fading effects.
Assignments:
1) Connect4 Game
2) Todo List Application
3) Implement atleast 2 case studies from w3schools how to
https://www.w3schools.com/howto/
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
158 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com