Professional Documents
Culture Documents
com/Gctpak
CIT-303 Web development with Java Instructor Name: Ms. saba
Chp.04 JavaScript
4.1.Javascript Introduction:
The development of JavaScript began in 1995 at Netscape Communications, the
makers of the Netscape browser. The first version of this language was actually
named Mocha in May 1995, renamed to LiveScript in September 1995, and
again renamed to JavaScript in December 1995.
Javascript definition:
It is primarily used to enhance web pages to provide for a more user friendly
experience. These include dynamically updating web pages, user interface
enhancements such as menus and dialog boxes, animations, 2D and 3D
graphics, interactive maps, video players, and more. This mode of JavaScript
usage in the web browser is also referred to as client-side javascript
Javascript syntax:
JavaScript syntax is the set of rules, how JavaScript programs are constructed:
Example 1:
<script>
document.getElementById("demo").innerHTML = "My First JavaScript";
</script>
Example 2:
<!DOCTYPE html>
<html>
www.youtube.com/AdeebTechnologyLab www.facebook.com/Adeeb.Technology.Lab
www.SalmanAdeeb.wixsite.com/DAE-CIT-books www.facebook.com/Gctpak
<body>
<h2>My First JavaScript</h2>
<button type="button"
onclick="document.getElementById('demo').innerHTML = Date()">
Click me to display Date and Time.</button>
<p id="demo"></p>
</body>
</html>
What can JavaScript do?
This example uses the method to "find" an HTML element (with id="demo") and
changes the element content (innerHTML) to "Hello JavaScript":
document.getElementById("demo").innerHTML = "Hello JavaScript";
Example:
<!DOCTYPE html>
<html>
<body>
<!DOCTYPE html>
<html>
<body>
<p>In this case JavaScript changes the value of the src (source) attribute of an
image.</p>
<button
onclick="document.getElementById('myImage').src='pic_bulbon.gif'">Turn on
the light</button>
<button
onclick="document.getElementById('myImage').src='pic_bulboff.gif'">Turn off
the light</button>
</body>
</html>
www.youtube.com/AdeebTechnologyLab www.facebook.com/Adeeb.Technology.Lab
www.SalmanAdeeb.wixsite.com/DAE-CIT-books www.facebook.com/Gctpak
3-JavaScript Can Hide HTML Elements:
Hiding HTML elements can be done by changing the display style:
<!DOCTYPE html>
<html>
<body>
<button type="button"
onclick="document.getElementById('demo').style.display='none'">Click
Me!</button>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<button type="button"
onclick="document.getElementById('demo').style.display='block'">Click
Me!</button>
</body>
www.youtube.com/AdeebTechnologyLab www.facebook.com/Adeeb.Technology.Lab
www.SalmanAdeeb.wixsite.com/DAE-CIT-books www.facebook.com/Gctpak
</html>
5-JavaScript Can Change HTML Styles (CSS):
Changing the style of an HTML element, is a variant of changing an HTML
attribute:
<!DOCTYPE html>
<html>
<body>
<button type="button"
onclick="document.getElementById('demo').style.fontSize='35px'">Click
Me!</button>
</body>
</html>
1-To hide an element with a specified id, use a hash tag (#) in front of the id
name.
<button onclick="w3.hide('#London')">Hide</button>
2-To hide all elements with a specified tag name, use the tag name (without <
and >) as the selector.
<button onclick="w3.hide('h2')">Hide</button>
www.youtube.com/AdeebTechnologyLab www.facebook.com/Adeeb.Technology.Lab
www.SalmanAdeeb.wixsite.com/DAE-CIT-books www.facebook.com/Gctpak
3-To hide all elements with a specified class name, use a period (.) in front of
the class name.
<button onclick="w3.hide('.city')">Hide</button>
JS Show Elements:
There are three methods to show elements :
showing Elements Using Element Id
showing Elements Using Tag Name
showing Elements Using class
1-To show an element with a specified id, use a hash tag (#) in front of the id
name.
<button onclick="w3.show('#London')">Show</button>
2-To show all elements with a specified tag name, use the tag name (without <
and >) as the selector.
<button onclick="w3.show('h2')">Show</button>
3-To show all elements with a specified class name, use a period (.) in front of
the class name.
<button onclick="w3.show('.city')">Show</button>
Example:
<!DOCTYPE html>
<html>
<title>W3.JS</title>
<script src="https://www.w3schools.com/lib/w3.js"></script>
<body>
<h2>Testing W3.JS in HTML</h2>
<p>Click the buttons to hide or show all elements with class="city".</p>
<p>
<button onclick="w3.hide('.city')">Hide</button>
<button onclick="w3.show('.city')">Show</button>
www.youtube.com/AdeebTechnologyLab www.facebook.com/Adeeb.Technology.Lab
www.SalmanAdeeb.wixsite.com/DAE-CIT-books www.facebook.com/Gctpak
</p>
<div id="London" class="city">
<h2>London</h2>
<p>London is the capital city of England.</p>
</div>
<div id="Paris" class="city">
<h2>Paris</h2>
<p>Paris is the capital of France.</p>
</div>
<div id="Tokyo" class="city">
<h2>Tokyo</h2>
<p>Tokyo is the capital of Japan.</p>
</div>
</body>
</html>
4.3.Definition of JQuery:
JQuery is a JavaScript library that allows web developers to add extra functionality
to their websites. It is open source and provided for free under the MIT license.
In recent years, JQuery has become the most popular JavaScript library used
in web development.
To implement JQuery, a web developer simply needs to reference the JQuery
JavaScript file within the HTML of a webpage.
jQuery is a fast, small, and feature-rich JavaScript library.
a webpage may load the JQuery library using the following line within the <head>
section of the HTML:
<script
www.youtube.com/AdeebTechnologyLab www.facebook.com/Adeeb.Technology.Lab
www.SalmanAdeeb.wixsite.com/DAE-CIT-books www.facebook.com/Gctpak
type="text/javascript"
src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
4.4.JQuery Selectors:
jQuery selectors are one of the most important parts of the jQuery library.
All selectors in jQuery start with the dollar sign and parentheses: $().
$("p")
Example
Example
$(document).ready(function(){
$("button").click(function(){
$("p").hide();
});
});
To find an element with a specific id, write a hash character, followed by the id
of the HTML element:
$("#test")
Example
When a user clicks on a button, the element with id="test" will be hidden:
Example
$(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:
$(".test")
When a user clicks on a button, the elements with class="test" will be hidden:
Example
$(document).ready(function(){
$("button").click(function(){
$(".test").hide();
});
});
Syntax Description
www.youtube.com/AdeebTechnologyLab www.facebook.com/Adeeb.Technology.Lab
www.SalmanAdeeb.wixsite.com/DAE-CIT-books www.facebook.com/Gctpak
$("ul li:first") Selects the first <li> element of the first <ul>
$("a[target='_blank']") Selects all <a> elements with a target attribute value equal to
"_blank"
$("a[target!='_blank']") Selects all <a> elements with a target attribute value NOT
equal to "_blank"
www.youtube.com/AdeebTechnologyLab www.facebook.com/Adeeb.Technology.Lab
www.SalmanAdeeb.wixsite.com/DAE-CIT-books www.facebook.com/Gctpak
4.5. Apply validation on HTML form by using Javascript:
In this example, we are going to validate the name and password. The name can’t be empty
and password can’t be less than 6 characters long.
Here, we are validating the form on form submit. The user will not be forwarded to the next
page until given values are correct.
<script>
function validateform(){
var name=document.myform.name.value;
var password=document.myform.password.value;
if (name==null || name==""){
alert("Name can't be blank");
return false;
}else if(password.length<6){
alert("Password must be at least 6 characters long.");
return false;
}
}
</script>
<body>
<form name="myform" method="post" action="abc.jsp" onsubmit="return validateform()"
>
Name: <input type="text" name="name"><br/>
Password: <input type="password" name="password"><br/>
<input type="submit" value="register">
</form>
if(firstpassword==secondpassword){
return true;
}
else{
alert("password must be same!");
return false;
www.youtube.com/AdeebTechnologyLab www.facebook.com/Adeeb.Technology.Lab
www.SalmanAdeeb.wixsite.com/DAE-CIT-books www.facebook.com/Gctpak
}
}
</script>
www.youtube.com/AdeebTechnologyLab www.facebook.com/Adeeb.Technology.Lab