Ch03 Javascript & jQuery-1
Ch03 Javascript & jQuery-1
JAVASCRIPT AND
JQUERY
Prof. Shital Pashankar
PHP already allows us to create dynamic web pages. Why also use client-side
scripting?
• client-side scripting (JavaScript) benefits:
• usability: can modify a page without having to post back to the server (faster UI)
• efficiency: can make small, quick changes to page without waiting for server
• event-driven: can respond to user actions like clicks and key presses
String Null
Data Types
Boolean Undefined
Object
Function RegExp
Array Date
• integers and real numbers are the same type (no int vs. double)
• same operators: + - * / % ++ -- = += -= *= /= %=
• similar precedence to Java
• many operators auto-convert types: "2" * 3 is 6
// single-line comment
/* multi-line comment */
JS
42 == 42.0 is true
"5.0" == 5 is true
var sum = 0;
for (var i = 0; i < 100; i++) {
sum = sum + i;
} JS
var s1 = "hello";
var s2 = "";
for (var i = 0; i < s1.length; i++) {
s2 += s1.charAt(i) + s1.charAt(i);
}
// s2 stores "hheelllloo" JS
do {
statements;
} while (condition);
JS
</script>
<script>
var r = parseInt(prompt('Enter the radius of a sphere: '));
// calculate the volume
var vol =( (Math.PI *Math.pow(r,3) )*4) / 3;
alert("The volume of the sphere is "+ vol);
</script>
<script>
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
d = new Date();
day = days[d.getDay()];
t = d.getHours();
if(t>=11){
t= t-12;
t = t +" PM : "+d.getMinutes()+ " : "+d.getSeconds(); }
else{
t = t +" AM : "+d.getMinutes()+ " : "+d.getSeconds(); }
alert("Today is = "+day+"\n Current time is = "+t);
</script>
function myFunction() {
alert("Hello!");
alert("How are you?");
} JS
Load Events
Mouse Events
function changeText() {
var span = document.getElementById("output");
var textBox = document.getElementById("textbox");
textbox.style.color = "red";
} JS
<!DOCTYPE html>
<html>
<body>
<button onclick= "getElementById('demo').innerHTML=Date()"> The time
is?</button>
<p id="demo">Result will appear here….</p>
</body>
</html>
<p>A function is triggered when the user is pressing a key in the input field.</p>
<input type="text" onkeypress="myFunction()">
</body></html>
• new Date(milliseconds) creates a new date object as zero time plus milliseconds
<script>
var time = new Date().getHours();
if (time < 10) {
document.write("Good morning ");
} else if (time < 20) {
document.write("Good day");
} else {
document.write("Good evening");
}
</script></body></html>
• With JavaScript, you can define and create your own objects.
• CDN Based Version − You can include jQuery library into your HTML code
directly from Content Delivery Network (CDN).
• You can include jQuery library into your HTML code directly from Content Delivery
Network (CDN). Google and Microsoft provides content deliver for the latest
version.
• We are using Google CDN version of the library throughout this chapter.
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 HTML element:
$(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:
$(document).ready(function(){
$("button").click(function(){
$(".test").hide();
});
});
$("ul li:first") Selects the first <li> element of the first <ul>
<body>
<p>Click on the square below:</p>
<span id = "result"> </span>
<div id = "division" style = "background-color:blue;">
This is Blue Square!!
</div>
</body>
</html>
<!DOCTYPE
html><html><head><script src="https://ajax.googleapis.com/ajax/
libs/jquery/3.7.1/jquery.min.js"></script><script>
$(document).ready(function(){
$("#hide").click(function(){
$("p").hide(); });
});
</script></head><body><p>If you click on the "Hide" button,
I will disappear.</p>
<button id="hide">Hide</button>
</body></html>
<!DOCTYPE html><html><head><script
src="jquery.min.js"></script><script>
$(document).ready(function(){
$("#hide").click(function(){
$("#p1").hide(); });
$("#show").click(function(){
$("#p1").show(); });
});
</script></head><body><p id="p1">If you click on the "Hide"
button, I will disappear.</p>
<button id="hide">Hide</button>
<button id="show">Show</button></body></html>
<!DOCTYPE html><html><head><script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js
"></script><script>
$(document).ready(function(){
$("button").click(function(){
var div = $("div");
div.animate({left: '100px'}, "slow");
div.animate({fontSize: '3em'},
"slow"); });});</script> </head><body>
<button>Start Animation</button>
<p>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!</p>
<div
style="background:#98bf21;height:100px;width:200px;position:absolute;
"> HELLO </div></body></html>
<!DOCTYPE html><html><head><script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
alert("Value: " + $("#test").val());
});
});
</script></head><body>
<p>Name: <input type="text" id="test" value="Mickey Mouse"></p>
<button>Show Value</button></body></html>