JQuery
jQuery¶
Plain javascript (Vanilla JS), we required to write everything on our own jQuery is js libray
Vanililla JS Code:¶
var myh1 = document.querySelectorAll('h1');
for(h1 of myh1){ h1.style.color = 'red'; }
jQuery:¶
$('h1').css('color','red')
- Jquery is a JS libray
-
It contain serval functions and objects to select/grab html element and to mainipulate styles very easily.
-
AJAX, Animations
- Browser compatibility
Advantages:¶
- Rich Library ... less code and do more
- readability
- clear and shorter code
- Ease of use
- AJAX support
- Cross Browser support
Limitation *¶
*** Youmightnotneedjquery.com***
- Whatever jquery is doing....js can do
- Performance effect
- just to use $() 10K line code need to load
How to connect with JQuery:¶
- By locally
- By CDN
- minified : for production
- no comment no spaces
- uncompressed: for developer
Element Selector id selector class selector * selector descendent selector adjucent selector attribute selector nth-of-type
h1:nth-of-type(odd){}
Manipulate HTML Elements:¶
$() ===> To select/Grab html element css() ==> To manipulate elements
$('li')
$('li:nth-of-type(2)').css('background', 'pink')
$('li:nth-of-type(odd)').css('background', 'red')
$('li:first')
$('li:first-od-type')
$('li':nth-of-type(1))
$('li').first()
$('li').last()
Q1. Write Vanilla Js and jQuery code to change all h1 tags text color as awhite and backgraound as red?
var allh1 = document.querySelectorAll('h1`)
for(h1 of allh1s){
h1.style.color = "white"
}
$('h1').css({color: "white', background: 'red'});
var all = document.querySelectorAll('li')
for(li of all){
li.style.fontSize = '20px';
}
$('li').css('fontSize', '20px')
- Select all paragraph tags and set background as green
-
Select all paragraph tags with class 'remaining' and make them 200px width
-
Select all paragrap
- Select only 3rd p tag and change fontsize as 30px