In the previous lecture, we learned:
In this lecture, we will continue with practical programming concepts and begin learning jQuery.
Topics covered:
A Nested If means an If statement inside another If statement.
It is used when multiple conditions need to be checked.
if(age >= 18)
{
if(country == "Pakistan")
{
alert("You are eligible");
}
}
Check Age
|
Yes
|
Check Country
|
Yes
|
Eligible
Nested If is useful when:
Switch Case is used when one variable can have multiple possible values.
Instead of writing many If-Else statements, we use Switch.
switch(variable)
{
case value1:
statement;
break;
case value2:
statement;
break;
default:
statement;
}
switch(country)
{
case "Pakistan":
alert("You are welcome");
break;
case "Israel":
alert("Illegal Country");
break;
default:
alert("Country Not Found");
}
Loops are used to repeat code.
The three most common loops are:
Used when the number of repetitions is known.
for(start; condition; increment)
{
statements;
}
for(i=1; i<=5; i++)
{
document.write(i);
}
1
2
3
4
5
Runs while a condition remains true.
while(condition)
{
statements;
}
var i = 1;
while(i <= 5)
{
document.write(i);
i++;
}
Runs at least one time before checking the condition.
do
{
statements;
}
while(condition);
var i = 1;
do
{
document.write(i);
i++;
}
while(i <= 5);
| Loop | Condition Check |
|---|---|
| For | Before execution |
| While | Before execution |
| Do While | After execution |
In PHP, associative arrays store data using keys.
Instead of numbers, meaningful names are used.
$student = array(
"Ali",
"Ahmed",
"Usman"
);
Access:
$student[0]
$student = array(
"name" => "Ali",
"age" => 27,
"city" => "Lahore"
);
Access:
$student["name"]
Arrays can contain other arrays.
$student = array();
$student[0][0] = "Ali";
$student[0][1] = 27;
$student[1][0] = "Ahmed";
$student[1][1] = 25;
Ali 27
Ahmed 25
Traversing means accessing every item inside an array.
var student = [
"Ali",
"Ahmed",
"Usman"
];
.length
Returns total number of items.
student.length
Result:
3
document.write(student.length);
Functions perform specific tasks.
A block of reusable code.
function welcome()
{
alert("Welcome");
}
A function that belongs to an object.
alert()
document.write()
student.length
Programming languages provide many ready-made functions.
Examples:
alert()
document.write()
parseInt()
parseFloat()
jQuery is full of ready-made functions.
Examples:
.hide()
.show()
.click()
.fadeIn()
.fadeOut()
These functions help us build interactive websites quickly.
A Function Signature generally consists of:
Function Name
+
Parameter List
abc(int a, int b)
Function Name:
abc
Parameters:
a
b
Tells the compiler that a function exists.
Example:
int abc(int a, int b);
Actual code of the function.
function abc(a,b)
{
var c;
c = a + b;
return c;
}
Executing a function.
Example:
abc(10,20);
Declaration
↓
Definition
↓
Call
An inline function performs declaration, definition and call together.
Everything is written in a compact format.
Example:
var total = function(a,b)
{
return a+b;
};
jQuery is a JavaScript library.
It contains thousands of ready-made functions.
jQuery helps us:
document.getElementById("box");
$("#box");
Much shorter and easier.
Selectors are used to target HTML elements.
$("#header")
Targets:
<div id="header"></div>
$(".box")
Targets:
<div class="box"></div>
$("p")
Targets:
<p>Hello</p>
The most important jQuery function.
It ensures the webpage loads before jQuery starts working.
$(document).ready(function()
{
});
$(function()
{
});
jQuery provides built-in animation functions.
$("#box").hide();
Hides an element.
$("#box").show();
Displays an element.
$("#btn").click();
Handles click events.
$("#box").fadeIn();
Gradually shows an element.
$("#box").fadeOut();
Gradually hides an element.
$("#box").slideUp();
Slides element upward.
$("#box").slideDown();
Slides element downward.
$("#box").slideToggle();
Switches between Slide Up and Slide Down.
These functions work with HTML content.
Gets or changes HTML.
$("#content").html();
Adds content at the end.
$("#content").append("New Data");
Adds content at the beginning.
$("#content").prepend("New Data");
Used to move through webpage elements.
Gets child elements.
$("#menu").children();
Gets parent element.
$("#menu").parent();
Gets next sibling.
$("#box").next();
Gets previous sibling.
$("#box").prev();
Gets all sibling elements.
$("#box").siblings();
Represents the current element.
$(this).hide();
If a button is clicked, only that button will be affected.
Used to get or change CSS properties.
$("#box").css("color","red");
$("#box").css({
color:"red",
background:"yellow"
});
In this lecture, we learned:
In the next lecture, we will build practical jQuery examples and learn how jQuery interacts with forms, menus, images, and webpage content.