JavaScript Fundamentals
Published:
This lesson covers JavaScript Lab Fundamentals.
Java Script Fundamentals - Lab
Examples
- Changing HTML content from java script
/* css/style.css */
h1{
color: red;
}
input[type=submit] {
width: 4em;
height: 1.5em;
font-size:2em;
}
<!-- index.html -->
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Hello JS</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body align="center">
<h1 id="hello"></h1>
<input type="submit" value="Hello" onclick="hello()">
<script>
function hello() {
document.getElementById("hello").innerHTML = "Welcome to My First Page";
}
</script>
</body>
</html>
- Checking length of entered username
/* css/style.css */
h1{
color: red;
}
input[type=submit] {
width: 4em;
height: 1.5em;
font-size:1.5em;
}
input[type=text] {
width: 10em;
height: 1.5em;
font-size:1.5em;
}
<!-- index.html -->
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Hello JS</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body align="center">
<h1> Welcome </h1>
<input type="text" name="username" id="username" placeholder="Enter username"> <br><br>
<input type="submit" name="submit" id="submit" value="Submit" onclick="submit()">
<script>
function submit() {
var username = document.getElementById("username").value
var username_len = username.length
if (username_len < 6){
alert("you entered username of "+ username_len + " chars, it should be atleast of 6 characters");
}
}
</script>
</body>
</html>
- JS Calculator
/* css/style.css */
h1{
color: red;
}
p {
font-size: 3em;
}
input[type=submit] {
width: 5em;
height: 1.5em;
font-size:1.5em;
}
input[type=radio] {
width: 1em;
height: 2em;
font-size:1.5em;
}
input[type=text] {
width: 10em;
height: 1.5em;
font-size:1.5em;
}
input[type=number] {
width: 15em;
height: 1.5em;
font-size:1.5em;
}
<!-- index.html -->
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Hello JS</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body align="center">
<h1> JS Calculator </h1>
<input type="number" name="num1" id="num1" placeholder="Enter first number"> <br><br>
<input type="number" name="num2" id="num2" placeholder="Enter second number"> <br><br>
<input type="radio" name="sign" value="addition" checked> Addition
<input type="radio" name="sign" value="subtraction" > Subtraction
<input type="radio" name="sign" value="multiplication"> Multiplication
<input type="radio" name="sign" value="division" > Division
<br><br>
<p id="result"></p>
<input type="submit" name="submit" id="submit" value="Compute" onclick="compute()">
<script>
function strip(number) {
return (parseFloat(number).toPrecision(3));
}
function compute() {
var num1 = document.getElementById("num1").value
var num2 = document.getElementById("num2").value
num1 = parseFloat(num1)
num2 = parseFloat(num2)
var sign = document.getElementsByName('sign');
var sign_name = ""
for(i=0; i<sign.length; i++) {
if(sign[i].checked)
var sign_name = sign[i].value;
}
var num = ""
if(sign_name === "addition")
num = num1 + num2
if(sign_name === "subtraction")
num = num1 - num2
if(sign_name === "multiplication")
num = num1 * num2
if(sign_name === "division")
num = num1 / num2
num = strip(num)
document.getElementById("result").innerHTML = " Result is " + num
}
</script>
</body>
</html>
- JS Calculator using external js file
/* css/style.css */
h1{
color: red;
}
p {
font-size: 3em;
}
input[type=submit] {
width: 5em;
height: 1.5em;
font-size:1.5em;
}
input[type=radio] {
width: 1em;
height: 2em;
font-size:1.5em;
}
input[type=text] {
width: 10em;
height: 1.5em;
font-size:1.5em;
}
input[type=number] {
width: 15em;
height: 1.5em;
font-size:1.5em;
}
/* js/script.js */
function strip(number) {
return (parseFloat(number).toPrecision(3));
}
function compute() {
var num1 = document.getElementById("num1").value
var num2 = document.getElementById("num2").value
num1 = parseFloat(num1)
num2 = parseFloat(num2)
var sign = document.getElementsByName('sign');
var sign_name = ""
for(i=0; i<sign.length; i++) {
if(sign[i].checked)
var sign_name = sign[i].value;
}
var num = ""
if(sign_name === "addition")
num = num1 + num2
if(sign_name === "subtraction")
num = num1 - num2
if(sign_name === "multiplication")
num = num1 * num2
if(sign_name === "division")
num = num1 / num2
num = strip(num)
document.getElementById("result").innerHTML = " Result is " + num
}
<!-- index.html -->
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Hello JS</title>
<link rel="stylesheet" href="css/style.css">
<script src="js/script.js"></script>
</head>
<body align="center">
<h1> JS Calculator </h1>
<input type="number" name="num1" id="num1" placeholder="Enter first number"> <br><br>
<input type="number" name="num2" id="num2" placeholder="Enter second number"> <br><br>
<input type="radio" name="sign" value="addition" checked> Addition
<input type="radio" name="sign" value="subtraction" > Subtraction
<input type="radio" name="sign" value="multiplication"> Multiplication
<input type="radio" name="sign" value="division" > Division
<br><br>
<p id="result"></p>
<input type="submit" name="submit" id="submit" value="Compute" onclick="compute()">
</body>
</html>