HTML, CSS, and JavaScript Basics
HTML, CSS, and JavaScript Basics
Horizontal Ruler: The <hr> tag defines a thematic break in an HTML page, and is most
often displayed as a horizontal rule.
Style Attribute: Setting the style of an HTML element, can be done with the style attribute.
Example: <h1 style = "color:blue;"> This is a heading </h1>
Images: Images can improve the design and the appearance of a web page.
Example: <img src="[Link]" width=”400px” height=”400px” alt="Italian Trulli"/>
Tables: HTML tables allow web developers to arrange data into rows and columns. The
<table> tag defines an HTML table. Each table row is defined with a <tr> tag. Each table header
is defined with a <th> tag. Each table data/cell is defined with a <td> tag.
Example
<table border="2">
<caption> III-IT STUDENTS DETAILS</caption>
<tr>
<th>SNO</th>
<th>SNAME</th>
<th>PERCENTAGE</th>
</tr>
<tr>
<td>1</td>
<td>SHIVA</td>
<td>96</td>
</tr>
<tr>
<td>1</td>
<td>SHIVA</td>
<td>96</td>
</tr>
</table>
Lists
HTML lists allow web developers to group a set of related items in lists.
Unordered List: starts with the <ul> tag. Each list item starts with the <li> tag.
Example
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
Ordered List: An ordered list starts with the <ol> tag. Each list item starts with the <li> tag.
Example
<ol>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
Frames
An HTML iframe is used to display a web page within a web page.
Helps us to view multiple pages at once.
Example:
<iframe name ="f1" src="[Link]" width="450" height="400"> </iframe>
Forms
An HTML form is used to collect user input. The user input is most often sent to a server for
processing.
The <input> Element:
<input type="text"> : Displays a single-line text input field
<input type="radio"> : Displays a radio button (for selecting one of many choices)
<input type="checkbox"> : Displays a checkbox (for selecting zero or more of many choices)
<input type="submit"> : Displays a submit button (for submitting the form)
<input type="button"> : Displays a button.
<input type="reset"> : Displays a reset button.
TextArea Element: The <textarea> element defines a multi-line input field (a text area):
Example:
<textarea name="message" rows="10" cols="30">
The cat was playing in the garden.
</textarea>
HTML-5 CANVAS:
• This element is used to draw graphics, on the fly, via with JavaScript.
• It is a container for graphics. You must use JavaScript to actually draw the graphics.
• It has several methods for drawing paths, boxes, circles, text, and adding images.
Example: Creating a rectangular canvas area and adding JavaScript code to draw a line on it.
<!DOCTYPE html>
<html>
<body>
<canvas id="myCanvas" width="200" height="100"
style="border:1px solid #d3d3d3;">
Your browser does not support the HTML canvas tag.
</canvas>
<script>
var c = [Link]("myCanvas");
var ctx = [Link]("2d");
[Link](0,0);
[Link](200,100);
[Link]();
</script>
</body>
</html>
[Link]
<html>
<head>
Datatypes:
There are two types of data types in JavaScript.
1. Primitive data type
2. Non-primitive (reference) data type
Variables:
• JavaScript is a dynamic type language, means you don't need to specify type of the
variable because it is dynamically assigned by JavaScript engine.
• You need to use “Var” or “let” here to specify the data type. It can hold any type of
values such as numbers, strings etc.
• Variables declared by var keyword are scoped to the immediate function body (hence
the function scope) while let variables are scoped to the immediate enclosing block
denoted by { } (hence the block scope).
Example:
var a = 40; //holding number
var b = "Rahul"; //holding string
Operators
4. Bitwise Operators: & (Bitwise AND), | (BitWise OR), ^ (Bitwise XOR), ~ (Bitwise Not), <<
(Left Shift), >> (Right Shift) and >>> (Right shift with Zero)
Example: 10<<2 // result = 40
5. Assignment Operators: = (Simple Assignment ), += , −=,*= , /= and %=
Literals:
• JavaScript Literals are the fixed values that cannot be changed, you do not need to
specify any type of keyword to write literals.
• Literals are often used to initialize variables in programming, names of variables are
string literals.
• A JavaScript Literal can be a numeric, string, floating-point value, a boolean value or
even an object.
• In simple words, any value is literal, if you write a string "Studytonight" is a literal, any
number like 7007 is a literal, etc.
“ if ” statement
The if statement is the fundamental control statement that allows JavaScript to make decisions
and execute statements conditionally.
Syntax:
if (expression) {
Statement(s) to be executed if expression is true
}
“ Switch ” Statement
The objective of a switch statement is to give an expression to evaluate and several different
statements to execute based on the value of the expression. The interpreter checks each case
against the value of the expression until a match is found. If nothing matches, a default condition
will be used.
Syntax
switch (expression)
{
case Label1: statement(s)
break;
case Label2: statement(s)
break;
default: statement(s)
}
Iterative Statements
while:
to execute a statement or code block repeatedly as long as an expression is true
Syntax:
while (expression)
{
Statement(s) to be executed if expression is true
}
do...while
Syntax:
do
{
Statement(s) to be executed;
} while (expression);
for
for (initialization; test condition; iteration statement)
{
Statement(s) to be executed if test condition is true
}
Functions
• A function is a group of reusable code which can be called anywhere in your program.
• This eliminates the need of writing the same code again and again.
• It helps programmers in writing modular codes.
• Functions allow a programmer to divide a big program into a number of small and
manageable functions.
Function Syntax:
function functionName([arg1, arg2, ...argN])
{
//code to be executed
}
Event: onclick
<html>
<head>
<script type = "text/javascript">
function sayHello()
{
alert("Hello World")
}
</script>
</head>
<body>
<form>
<input type = "button" onclick = "sayHello()" value = "Say Hello" />
</form>
</body>
</html>
Arrays
• The Array object lets you store multiple values in a single variable.
• It stores a fixed-size sequential collection of elements of same type.
• An array is used to store a collection of data, but it is often more useful to think of an
array as a collection of variables of the same type.
Example:
var fruits = [ "apple", "orange", "mango"]);
or
var fruits = new Array( "apple", "orange", "mango" );
or
var fruits = new Array();
fruits[0] = "apple";
fruits[1] = "orange";
fruits[2] = "mango";
Methods:
1. concat(): Returns a new array comprised of this array joined with other array.
Example: var arr1 = ["C","C++","Python"];
var arr2 = ["Java","JavaScript","Android"];
var result = [Link](arr2);
2. indexOf(): Returns the first index of an element within the array equal to the specified value,
or -1 if none is found.
Example: var arr = ["C","C++","Python","C++","Java"];
var result = [Link]("C++");
3. pop(): Removes the last element from an array.
Example: [Link]()
4. shift(): Removes the first element from an array.
Example: [Link]()
5. push(): Adds one or more elements to the end of an array.
Example: [Link]("JQuery");
6. unshift(): Adds one or more elements to the front of an array.
Example: [Link](“JQuery”)
7. reverse(): Reverses the order of the elements of an array.
Example: [Link]()
8. sort(): Sorts the elements of an array
Example: [Link]()
9. slice(): Extracts a section of an array and returns a new array.
Example: var arr = ["AngularJS","[Link]","JQuery","Bootstrap"]
var result = [Link](1,2);
JavaScript Objects
String Object
The String object lets you work with a series of characters; it wraps Javascript's string primitive
data type with a number of helper methods.
Example: var str = "shiva"; or var str = new String("shiva");
Methods:
[Link](): Returns the character at the specified index.
Example: var str = "Javatpoint";
[Link]( [Link](4) );
2. concat(): Combines the text of two strings and returns a new string.
Example: var x = "Javatpoint";
var y = ".com";
[Link]( [Link](y) );
3. toLowerCase(): Returns the calling string value converted to lower case.
Example: var str = "JAVATPOINT";
[Link]( [Link]() );
4. toUpperCase(): Returns the calling string value converted to uppercase.
5. indexOf(): Returns the index within the calling String object of the first occurrence of the
specified value, or -1 if not found.
Example: var web = "Learn JavaScript on Javatpoint" ;
[Link]( [Link]('a') );
6. lastIndexOf(): It provides the position of a char value present in the given string by searching
a character from the last position.
7. replace(): It replaces a given string with the specified replacement.
Example: var str = "Javatpoint" ;
[Link]( [Link]("tpoint","Script") );
8. substr(): It is used to fetch the part of the given string on the basis of the specified starting
position and length.
Example: var str = "Javatpoint" ;
[Link]( [Link](0,4) );
9. substring(): It is used to fetch the part of the given string on the basis of the specified index.
Example: var str = "Javatpoint";
[Link]( [Link](0,4) );
10. trim(): It trims the white space from the left and right side of the string.
Math Object
• The math object provides you properties and methods for mathematical constants and
functions.
• All the properties and methods of Math are static and can be called by using Math as an
object without creating it.
Methods:
1. abs(): Returns the absolute value of a number.
Example: [Link]( [Link](-4) )
2. ceil(): Returns the smallest integer greater than or equal to a number.
Example: [Link]( [Link](7.2) ); // 8
3. floor(): Returns the largest integer less than or equal to a number.
Example: [Link]( [Link](7.2) ); // 7
4. round(): Returns the value of a number rounded to the nearest integer.
Example: [Link]( [Link](7.2) ); // 7
5. exp(): It returns the exponential form of the given number.
Example: [Link]( [Link](1) ); // 2.718281828459045
6. log(): Returns the natural logarithm (base E) of a number.
Example: [Link]( [Link](5) ); // 1.6094379124341003
7. max(): Returns the largest number.
Example: [Link]( [Link](22,34,12,15) );
8. min(): Returns the smallest of zero or more numbers.
9. pow(): Returns base to the exponent power, that is, base exponent.
Example: [Link]( [Link](2,3) );
10. random(): Returns a pseudo-random number between 0 and 1.
Example: [Link]( [Link]() )
11. sqrt(): Returns the square root of a number.
Example: [Link]([Link](16))
12. sin(): Returns the sine of a number.
13. tan(): Returns the tangent of a number.
14. cos(): Returns the cosine of a number..
Date Object:
• The Date object is a datatype built into the JavaScript language.
• Date objects are created with the new operator.
Methods:
1. getDate(): It returns the integer value between 1 and 31 that represents the day for the
specified date.
Example: var d = new Date() ;
[Link]( [Link]() ) ;
2. getDay(): It returns the integer value between 0 and 6 that represents the day of the week
Example: [Link]( [Link]() );
3. getFullYear(): Returns the year of the specified date.
4. getHours(): It returns the integer value between 0 and 23 that represents the hours
5. getMilliseconds(): It returns the integer value between 0 and 999 that represents the
milliseconds
6. getMinutes(): It returns the integer value between 0 and 59 that represents the minutes
7. getMonth(): It returns the integer value between 0 and 11 that represents the month
8. getSeconds(): It returns the integer value between 0 and 60 that represents the seconds
Boolean Object
• JavaScript Boolean is an object that represents value in two states: true or false.
• You can create the JavaScript Boolean object by Boolean() constructor as given below.
Boolean b = new Boolean(value);
• The default value of JavaScript Boolean object is false.
Methods
toSource(): returns the source of Boolean object as a string.
toString(): converts Boolean into String.
valueOf(): converts other type into Boolean.
Number Object
• The JavaScript number object enables you to represent a numeric value.
• It may be integer or floating-point.
var n = new Number(value);
Methods:
isInteger() It determines whether the given value is an integer or not.
parseFloat() It converts the given string into a floating point number.
parseInt() It converts the given string into an integer number.
toExponential() It returns the string that represents exponential notation of the given number.
toPrecision() It returns the string representing a number of specified precision.
toString() It returns the given number in the form of string.
window object
• The window object represents a window in browser.
• An object of window is created automatically by the browser.
Methods of window object
alert() displays the alert box containing message with ok button.
confirm() displays the confirm dialog box containing message with ok and cancel button.
prompt() displays a dialog box to get input from the user.
open() opens the new window.
close() closes the current window.
document object ( Document Object Model)
• When html document is loaded in the browser, it creates a hierarchical tree structure with
all its elements.
• The document object is the root element.
• The document object represents the whole html document
• DOM allows us to dynamically access and update the content, structure, and style of a
document.
• It is the object of window. So can access as [Link] or document.
Properties:
1. bgcolor: indicates the background color of the document
2. fgcolor: indicates foreground color(text color)
3. anchors[]: an array containing references to all named anchors(locations).
4. forms[]: an array of the forms contained in the current document
5. images[]: an array containing references to all images in the current document
6. link[]: an array containg referenes to all the links
7. title: the title of the document as defined between <title></title>
8. url: the url of the current document object
Methods:
write("string") writes the given string on the doucment.
writeln("string") writes the given string on document with newline character at end
getElementById() returns the element having the given id value.
getElementsByName() returns all the elements having the given name value.
getElementsByTagName(): returns all the elements having the given tag name.
Navigator Object
• It can be used to get browser information such as browser name, version, supported mime
types.. etc.
• The navigator object is the window property, so it can be accessed by:
[Link] Or navigator
Properties:
appName: returns the name of the browser
appCodeName: returns internal name of the browser
appVersion: returns the version
cookieEnabled: returns true if cookie is enabled otherwise false
userAgent: returns the appName plus appVersion
mimeTypes[]: returns the array of mime types.
platform: returns the platform e.g. Win32.
Form Validations
validation for --- “username” field:
<html>
<head>
<script>
function validate()
{
var u = [Link];
if( [Link] == 0 )
alert('user name cant be empty');
else if ( [Link] < 8 )
alert('user name should be atleast 8 characters');
else
{
if (  )
alert('invalid mail id');
}