SlideShare a Scribd company logo
10 JavaScript Projects - Laurence
Svekis
requestAnimationFrame and
cancelAnimationFrame Code Sample
<!doctype html><html>
<head>
<title>Questions and Answers JavaScript</title>
</head>
<body>
<div class="top">
<div class="nested1">Nested 1</div>
<div class="nested2">Nested 2</div>
<div class="nested3">Nested 3</div>
</div>
<script>
let tog = true;
const div = document.createElement('div');
div.textContent = "hello";
div.style.color = "red";
div.style.position = "absolute";
div.style.left = '50px';
div.x = 50;
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
div.addEventListener('click', stopper);
const topEle = document.querySelector('.top');
topEle.append(div);
let myAni = requestAnimationFrame(mover);
function stopper() {
if (tog) {
cancelAnimationFrame(myAni);
tog = false;
}
else {
tog = true;
myAni = requestAnimationFrame(mover);
}
}
function mover() {
div.x = div.x + 1;
div.style.left = div.x + 'px';
myAni = requestAnimationFrame(mover);
}
</script>
</body>
</html>
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
JavaScript Switch Statement
<!doctype html>
<html>
<head>
<title>Questions and Answers JavaScript</title>
</head>
<body>
<div class="top">
<div class="nested1">Nested 1</div>
<div class="nested2">Nested 2</div>
<div class="nested3">Nested 3</div>
</div>
<div class="message">What time is it</div>
<input type="text">
<button>Click</button>
<script>
const btn = document.querySelector('button');
const answer = document.querySelector('input');
const message = document.querySelector('.message');
btn.addEventListener('click', function () {
console.log(answer.value);
//let ans = Number(answer.value);
let ans = parseInt(answer.value);
//console.log(typeof(answer.value));
console.log(typeof (ans));
console.log(ans);
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
if (!Number(ans)) {
console.log('not a number');
}
else {
console.log('Okay');
message.textContent = checkTimeofDay(ans);
}
})
outputToday();
function outputToday() {
const today = new Date().getDay();
let dayName = 'Unknown';
let weekStatus = 'Unknown';
switch (today) {
case 0:
dayName = "Sunday";
break;
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
case 4:
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
dayName = "Thursday";
break;
case 5:
dayName = "Friday";
break;
case 6:
dayName = "Saturday";
break;
}
switch (dayName) {
case "Thursday":
case "Friday":
case "Saturday":
weekStatus = "end of Week";
break;
default:
weekStatus = "Start of Week";
}
console.log(today);
message.textContent = `Today is a ${dayName} its the
${weekStatus}`;
}
function checkTimeofDay(num) {
switch (num < 12) {
case true:
return 'Good Morning';
break;
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
case false:
return 'Good Afternoon';
break;
default:
return 'something went wrong'
}
}
</script>
</body>
</html>
Example of using Continue and Break in
For loop and While Loop
<!doctype html>
<html>
<head>
<title>Questions and Answers JavaScript</title>
</head>
<body>
<div class="top">
<div class="nested1">Nested 1</div>
<div class="nested2">Nested 2</div>
<div class="nested3">Nested 3</div>
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
</div>
<div class="message">What time is it</div>
<input type="text">
<button>Click</button>
<script>
for (let i = 0; i < 10; i++) {
if (i === 3) {
continue;
}
if (i === 8) {
break;
}
console.log(i);
}
let x = 0;
while (x < 10) {
//if(x===3){continue;}
if (x === 8) {
break;
}
//console.log(x);
x++;
}
//console.log(x);
</script>
</body>
</html>
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
Keyboard Event Listeners - Dynamically
Add Page Elements input and divs
<!doctype html><html>
<head>
<title>Questions and Answers JavaScript</title>
</head>
<body>
<script>
const output = document.createElement('div');
const message = document.createElement('div');
const btn = document.createElement('button');
document.body.append(output);
output.append(message);
output.append(btn);
btn.textContent = "Click to add input";
btn.style.backgroundColor = 'red';
btn.style.color = 'white';
btn.style.padding = '10px';
btn.addEventListener('click', maker)
function maker() {
const tempDiv = document.createElement('div');
const newInput = document.createElement('input');
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
output.append(tempDiv);
tempDiv.append(newInput);
newInput.value = 'test';
newInput.hiddenValue =
Math.random().toString(16).substr(-6);
newInput.style.backgroundColor = '#' +
newInput.hiddenValue;
newInput.focus();
newInput.addEventListener('keyup', log);
newInput.addEventListener('keypress', log);
newInput.addEventListener('keydown', function (e) {
console.log(e.keyCode);
if (e.keyCode == 13) {
message.innerHTML += `<div
style="background:#${newInput.hiddenValue}">${newInput.value}</d
iv>`;
}
});
}
function log(event) {
console.log(event);
}
</script>
</body>
</html>
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
Create Page Elements add Dynamically
on the Page
<!doctype html><html>
<head>
<title>Questions and Answers JavaScript</title>
</head>
<body>
<script>
const btn = document.createElement('button');
const output = document.createElement('div');
const message = document.createElement('div');
btn.textContent = "Click Me!";
message.textContent = "Hello World";
document.body.append(output);
output.append(message);
output.append(btn);
btn.addEventListener('click', () => {
const today = new Date();
message.textContent = `${today.getHours()}
${today.getMinutes()} ${today.getSeconds()}`;
})
</script>
</body>
</html>
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
Pure JavaScript Dice - Create Elements
and Build HTML for Dice
<!doctype html><html>
<head>
<title>Questions and Answers JavaScript</title>
</head>
<body>
<script>
const diceView = [[5], [1, 9], [1, 5, 9], [1, 3, 7, 9],
[1, 3, 5, 7, 9], [1, 3, 4, 6, 7, 9]];
const btn = document.createElement('button');
btn.textContent = "Roll Dice";
const playArea = document.createElement('div');
document.body.prepend(playArea);
playArea.append(btn);
const area1 = document.createElement('div');
const area2 = document.createElement('div');
const container = document.createElement('div');
playArea.append(container);
container.append(area1);
container.append(area2);
area1.textContent = "first Dice";
area2.textContent = "second Dice";
addBorders(area1);
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
addBorders(area2);
btn.addEventListener('click', () => {
rollValue();
console.log(area1.val);
console.log(area2.val);
})
function genDice(val) {
let html = '<div>';
let tempArr = diceView[val];
console.log(tempArr);
for (let x = 1; x < 10; x++) {
let tempVal = 'white';
if (tempArr.includes(x)) {
tempVal = 'black';
}
html += `<span
style="width:90px;display:inline-block;height:90px;border-radius
:20px;background-color:${tempVal};margin:2px;"></span>`;
}
html += '</div>';
return html;
}
function rollValue() {
area1.val = Math.floor(Math.random() * 6);
area2.val = Math.floor(Math.random() * 6);
area1.innerHTML = genDice(area1.val);
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
area2.innerHTML = genDice(area2.val);
}
function addBorders(el) {
el.style.border = '1px solid #ddd';
el.style.borderRadius = "10px";
el.style.padding = '10px';
el.style.fontSize = '1.5em';
el.style.width = '290px';
el.style.height = '290px';
el.style.margin = '10px';
el.style.backgroundColor = 'white';
//el.style.width = '40%';
el.style.float = 'left';
//el.style.height = el.offsetWidth+'px';
}
</script>
</body>
</html>
Create a JavaScript popup Modal
<!doctype html><!doctype html>
<html>
<head>
<title>Course</title>
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
<style>
.modal {
position: fixed;
z-index: 5;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-color: rgb(0, 0, 0);
background-color: rgba(0, 0, 0, 0.3);
display: none;
}
.modal-body {
background-color: white;
margin: 20% auto;
padding: 20px;
border: 1px solid #333;
border-radius: 25px;
width: 70%;
min-height: 200px;
}
.close {
float: right;
color: red;
font-size: 2em;
font-weight: bold;
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
}
.close:hover {
color: black;
cursor: pointer;
}
</style>
</head>
<body>
<button class='modal1'>Open 1</button>
<button class='modal1'>Open 2</button>
<div class="modal" id="main">
<div class="modal-body"> <span class="close">&times;</span>
<div class="modal-text">Modal Text
<br> test </div>
</div>
</div>
<script>
const btns = document.querySelectorAll('.modal1');
const output = document.querySelector('.modal-text');
btns.forEach((btn) => {
btn.addEventListener('click', (e) => {
myModal.style.display = 'block';
console.log(e.target.textContent);
let val = e.target.textContent;
let html = "";
switch (val) {
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
case 'Open 1':
html = 'Number one is open <h1>ONE</h1>';
break;
case 'Open 2':
html = '<h1>TWO</h1>';
break;
default:
html = '<h1>ERROR</h1>';
}
output.innerHTML = html;
})
})
const closer = document.querySelector('.close');
const myModal = document.querySelector('#main');
closer.addEventListener('click', closeModal);
myModal.addEventListener('click', closeModal);
function closeModal() {
myModal.style.display = 'none';
}
</script>
</body>
</html>
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
JavaScript Request Animation Frame
Simple Counter
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<h1>Hello World</h1>
<script>
const output = document.querySelector('h1');
output.textContent = 'Counter';
let reqVal = requestAnimationFrame(step);
let start;
function step(cnt) {
console.log(cnt);
if (start == undefined) {
start = cnt;
}
const val = Math.floor(cnt - start);
const str = String(val);
console.log(str[0]);
const mil = str.slice(1, 4);
console.log(mil);
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
console.log(val);
output.textContent = `${str[0]} : ${mil}`;
if (val < 5000) {
reqVal = requestAnimationFrame(step);
}
}
</script>
</body>
</html>
QuerySelector adding elements
dynamically to page use of NodeList
<!doctype html>
<html>
<head>
<title>Example querySelectorAll</title>
</head>
<body>
<ul></ul>
<input type="text" name="myInput" value="test">
<button>Click Me to add item</button>
<script>
const ul = document.querySelector('ul');
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
const li = document.querySelectorAll('li');
const myInput =
document.querySelector('input[name="myInput"]');
const btn = document.querySelector('button');
let x = 0;
let val = myInput.value;
btn.addEventListener('click', (e) => {
//console.log(e);
x++;
e.target.textContent = 'Clicked ' + x;
addListItem();
})
function addListItem() {
//console.log(myInput.value);
//console.log(val);
console.dir(ul);
console.log(ul.children.length);
console.log(ul.childElementCount);
const lis = document.querySelectorAll('li');
//console.log(lis.length);
if (myInput.value.length > 3 && lis.length < 5) {
const li = document.createElement('li');
li.textContent = myInput.value;
const val1 = ul.appendChild(li);
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
//console.log(val1);
}
}
</script>
</body>
</html>
Adding Event Listeners to All Matching
Elements on Page - Dynamically adding
<!doctype html>
<html>
<head>
<title>Example querySelectorAll Click</title>
<style>
.active {
color: blue;
}
</style>
</head>
<body>
<h1>Hello</h1>
<ul class="myList">
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
<script>
const ul = document.querySelector('ul.myList');
const lis = ul.querySelectorAll('li');
const btn = document.createElement('button');
let counter = lis.length;
btn.textContent = 'Click Me';
document.body.append(btn);
btn.addEventListener('click', (e) => {
counter++;
const li = document.createElement('li');
li.acter = 0;
li.textContent = `Value (${counter}) ${li.acter} - `;
li.addEventListener('click', updateItem);
ul.append(li);
})
lis.forEach((li) => {
console.log(li);
li.acter = 0;
li.addEventListener('click', updateItem);
})
function updateItem(e) {
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
const ele = e.target;
console.dir(ele);
ele.acter++;
console.log(ele.acter);
let temp = ele.textContent;
ele.textContent = `${temp} ${ele.acter}`;
ele.classList.toggle('active');
console.log(ele.classList.contains('active'));
}
</script>
</body>
</html>
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/

More Related Content

What's hot (20)

PDF
JAVA SCRIPT
Mohammed Hussein
 
PPTX
Java script
Sukrit Gupta
 
PDF
Why and How to Use Virtual DOM
Daiwei Lu
 
PPT
Java script
umesh patil
 
PDF
22 j query1
Fajar Baskoro
 
PDF
How to make Ajax work for you
Simon Willison
 
PDF
Develop High Performance Windows 8 Application with HTML5 and JavaScriptHigh ...
Doris Chen
 
PDF
Web Projects: From Theory To Practice
Sergey Bolshchikov
 
PDF
Data presentation with dust js technologies backing linkedin
Ruhaim Izmeth
 
PDF
Web Components
Nikolaus Graf
 
PDF
1 ppt-ajax with-j_query
Fajar Baskoro
 
PDF
Front End Development: The Important Parts
Sergey Bolshchikov
 
PDF
Difference between java script and jquery
Umar Ali
 
PPT
J query b_dotnet_ug_meet_12_may_2012
ghnash
 
PDF
Introduction to web components
Marc Bächinger
 
PPTX
Mdst 3559-02-10-jquery
Rafael Alvarado
 
PPT
Web Performance Tips
Ravi Raj
 
PPTX
JavaScript and jQuery Basics
Kaloyan Kosev
 
PDF
JavaScript and BOM events
Jussi Pohjolainen
 
PDF
Better Selenium Tests with Geb - Selenium Conf 2014
Naresha K
 
JAVA SCRIPT
Mohammed Hussein
 
Java script
Sukrit Gupta
 
Why and How to Use Virtual DOM
Daiwei Lu
 
Java script
umesh patil
 
22 j query1
Fajar Baskoro
 
How to make Ajax work for you
Simon Willison
 
Develop High Performance Windows 8 Application with HTML5 and JavaScriptHigh ...
Doris Chen
 
Web Projects: From Theory To Practice
Sergey Bolshchikov
 
Data presentation with dust js technologies backing linkedin
Ruhaim Izmeth
 
Web Components
Nikolaus Graf
 
1 ppt-ajax with-j_query
Fajar Baskoro
 
Front End Development: The Important Parts
Sergey Bolshchikov
 
Difference between java script and jquery
Umar Ali
 
J query b_dotnet_ug_meet_12_may_2012
ghnash
 
Introduction to web components
Marc Bächinger
 
Mdst 3559-02-10-jquery
Rafael Alvarado
 
Web Performance Tips
Ravi Raj
 
JavaScript and jQuery Basics
Kaloyan Kosev
 
JavaScript and BOM events
Jussi Pohjolainen
 
Better Selenium Tests with Geb - Selenium Conf 2014
Naresha K
 

Similar to 10 java script projects full source code (20)

DOC
14922 java script built (1)
dineshrana201992
 
PPTX
Loops in java script
Ravi Bhadauria
 
PPTX
Java script
bosybosy
 
PPTX
10. session 10 loops and arrays
Phúc Đỗ
 
PDF
Introduction to web programming with JavaScript
T11 Sessions
 
PPTX
Java Script
Kalidass Balasubramaniam
 
PDF
The Evolution of Async-Programming (SD 2.0, JavaScript)
jeffz
 
PDF
Javascript
Adil Jafri
 
PDF
The Future of JavaScript (SXSW '07)
Aaron Gustafson
 
PPTX
BITM3730 10-17.pptx
MattMarino13
 
PPTX
Week 7 html css js
brianjihoonlee
 
PPTX
First javascript workshop : first basics
GhassenZrigua1
 
PDF
27javascript
Adil Jafri
 
PPTX
All you need to know about the JavaScript event loop
Saša Tatar
 
PPTX
JavaScript Session 3
Muhammad Ehtisham Siddiqui
 
PDF
CoffeeScript
Scott Leberknight
 
PDF
Jscex: Write Sexy JavaScript
jeffz
 
PDF
Lecture 03 - JQuery.pdf
Lê Thưởng
 
DOCX
program logbook 2
Jack Eastwood
 
PPTX
Javascript basics for automation testing
Vikas Thange
 
14922 java script built (1)
dineshrana201992
 
Loops in java script
Ravi Bhadauria
 
Java script
bosybosy
 
10. session 10 loops and arrays
Phúc Đỗ
 
Introduction to web programming with JavaScript
T11 Sessions
 
The Evolution of Async-Programming (SD 2.0, JavaScript)
jeffz
 
Javascript
Adil Jafri
 
The Future of JavaScript (SXSW '07)
Aaron Gustafson
 
BITM3730 10-17.pptx
MattMarino13
 
Week 7 html css js
brianjihoonlee
 
First javascript workshop : first basics
GhassenZrigua1
 
27javascript
Adil Jafri
 
All you need to know about the JavaScript event loop
Saša Tatar
 
JavaScript Session 3
Muhammad Ehtisham Siddiqui
 
CoffeeScript
Scott Leberknight
 
Jscex: Write Sexy JavaScript
jeffz
 
Lecture 03 - JQuery.pdf
Lê Thưởng
 
program logbook 2
Jack Eastwood
 
Javascript basics for automation testing
Vikas Thange
 
Ad

More from Laurence Svekis ✔ (19)

PDF
Quiz JavaScript Objects Learn more about JavaScript
Laurence Svekis ✔
 
PDF
JavaScript Lessons 2023 V2
Laurence Svekis ✔
 
PDF
JavaScript Lessons 2023
Laurence Svekis ✔
 
PDF
Top 10 Linkedin Tips Guide 2023
Laurence Svekis ✔
 
PDF
JavaScript Interview Questions 2023
Laurence Svekis ✔
 
PDF
Code examples javascript ebook
Laurence Svekis ✔
 
PDF
Brackets code editor guide
Laurence Svekis ✔
 
PDF
Web hosting get start online
Laurence Svekis ✔
 
PDF
JavaScript guide 2020 Learn JavaScript
Laurence Svekis ✔
 
PDF
Web hosting Free Hosting
Laurence Svekis ✔
 
PDF
Web development resources brackets
Laurence Svekis ✔
 
PPTX
Google Apps Script for Beginners- Amazing Things with Code
Laurence Svekis ✔
 
PDF
Introduction to Node js for beginners + game project
Laurence Svekis ✔
 
PPTX
JavaScript Advanced - Useful methods to power up your code
Laurence Svekis ✔
 
PPTX
JavaScript Objects and OOP Programming with JavaScript
Laurence Svekis ✔
 
PPTX
JavaScript Core fundamentals - Learn JavaScript Here
Laurence Svekis ✔
 
PPTX
Web Development Introduction to jQuery
Laurence Svekis ✔
 
PPTX
WordPress for Entrepreneurs Management of your own website
Laurence Svekis ✔
 
PPTX
Getting to Know Bootstrap for Rapid Web Development
Laurence Svekis ✔
 
Quiz JavaScript Objects Learn more about JavaScript
Laurence Svekis ✔
 
JavaScript Lessons 2023 V2
Laurence Svekis ✔
 
JavaScript Lessons 2023
Laurence Svekis ✔
 
Top 10 Linkedin Tips Guide 2023
Laurence Svekis ✔
 
JavaScript Interview Questions 2023
Laurence Svekis ✔
 
Code examples javascript ebook
Laurence Svekis ✔
 
Brackets code editor guide
Laurence Svekis ✔
 
Web hosting get start online
Laurence Svekis ✔
 
JavaScript guide 2020 Learn JavaScript
Laurence Svekis ✔
 
Web hosting Free Hosting
Laurence Svekis ✔
 
Web development resources brackets
Laurence Svekis ✔
 
Google Apps Script for Beginners- Amazing Things with Code
Laurence Svekis ✔
 
Introduction to Node js for beginners + game project
Laurence Svekis ✔
 
JavaScript Advanced - Useful methods to power up your code
Laurence Svekis ✔
 
JavaScript Objects and OOP Programming with JavaScript
Laurence Svekis ✔
 
JavaScript Core fundamentals - Learn JavaScript Here
Laurence Svekis ✔
 
Web Development Introduction to jQuery
Laurence Svekis ✔
 
WordPress for Entrepreneurs Management of your own website
Laurence Svekis ✔
 
Getting to Know Bootstrap for Rapid Web Development
Laurence Svekis ✔
 
Ad

Recently uploaded (20)

PPTX
infertility, types,causes, impact, and management
Ritu480198
 
PDF
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
PDF
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
PPTX
Introduction to Indian Writing in English
Trushali Dodiya
 
PPTX
Controller Request and Response in Odoo18
Celine George
 
PPTX
Light Reflection and Refraction- Activities - Class X Science
SONU ACADEMY
 
PPTX
ENG8_Q1_WEEK2_LESSON1. Presentation pptx
marawehsvinetshe
 
PPTX
ENGlish 8 lesson presentation PowerPoint.pptx
marawehsvinetshe
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PPTX
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
PPT
Indian Contract Act 1872, Business Law #MBA #BBA #BCOM
priyasinghy107
 
PPTX
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
PDF
Council of Chalcedon Re-Examined
Smiling Lungs
 
PDF
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
infertility, types,causes, impact, and management
Ritu480198
 
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
Introduction to Indian Writing in English
Trushali Dodiya
 
Controller Request and Response in Odoo18
Celine George
 
Light Reflection and Refraction- Activities - Class X Science
SONU ACADEMY
 
ENG8_Q1_WEEK2_LESSON1. Presentation pptx
marawehsvinetshe
 
ENGlish 8 lesson presentation PowerPoint.pptx
marawehsvinetshe
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
Indian Contract Act 1872, Business Law #MBA #BBA #BCOM
priyasinghy107
 
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
Council of Chalcedon Re-Examined
Smiling Lungs
 
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 

10 java script projects full source code

  • 1. 10 JavaScript Projects - Laurence Svekis requestAnimationFrame and cancelAnimationFrame Code Sample <!doctype html><html> <head> <title>Questions and Answers JavaScript</title> </head> <body> <div class="top"> <div class="nested1">Nested 1</div> <div class="nested2">Nested 2</div> <div class="nested3">Nested 3</div> </div> <script> let tog = true; const div = document.createElement('div'); div.textContent = "hello"; div.style.color = "red"; div.style.position = "absolute"; div.style.left = '50px'; div.x = 50; Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 2. div.addEventListener('click', stopper); const topEle = document.querySelector('.top'); topEle.append(div); let myAni = requestAnimationFrame(mover); function stopper() { if (tog) { cancelAnimationFrame(myAni); tog = false; } else { tog = true; myAni = requestAnimationFrame(mover); } } function mover() { div.x = div.x + 1; div.style.left = div.x + 'px'; myAni = requestAnimationFrame(mover); } </script> </body> </html> Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 3. JavaScript Switch Statement <!doctype html> <html> <head> <title>Questions and Answers JavaScript</title> </head> <body> <div class="top"> <div class="nested1">Nested 1</div> <div class="nested2">Nested 2</div> <div class="nested3">Nested 3</div> </div> <div class="message">What time is it</div> <input type="text"> <button>Click</button> <script> const btn = document.querySelector('button'); const answer = document.querySelector('input'); const message = document.querySelector('.message'); btn.addEventListener('click', function () { console.log(answer.value); //let ans = Number(answer.value); let ans = parseInt(answer.value); //console.log(typeof(answer.value)); console.log(typeof (ans)); console.log(ans); Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 4. if (!Number(ans)) { console.log('not a number'); } else { console.log('Okay'); message.textContent = checkTimeofDay(ans); } }) outputToday(); function outputToday() { const today = new Date().getDay(); let dayName = 'Unknown'; let weekStatus = 'Unknown'; switch (today) { case 0: dayName = "Sunday"; break; case 1: dayName = "Monday"; break; case 2: dayName = "Tuesday"; break; case 3: dayName = "Wednesday"; break; case 4: Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 5. dayName = "Thursday"; break; case 5: dayName = "Friday"; break; case 6: dayName = "Saturday"; break; } switch (dayName) { case "Thursday": case "Friday": case "Saturday": weekStatus = "end of Week"; break; default: weekStatus = "Start of Week"; } console.log(today); message.textContent = `Today is a ${dayName} its the ${weekStatus}`; } function checkTimeofDay(num) { switch (num < 12) { case true: return 'Good Morning'; break; Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 6. case false: return 'Good Afternoon'; break; default: return 'something went wrong' } } </script> </body> </html> Example of using Continue and Break in For loop and While Loop <!doctype html> <html> <head> <title>Questions and Answers JavaScript</title> </head> <body> <div class="top"> <div class="nested1">Nested 1</div> <div class="nested2">Nested 2</div> <div class="nested3">Nested 3</div> Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 7. </div> <div class="message">What time is it</div> <input type="text"> <button>Click</button> <script> for (let i = 0; i < 10; i++) { if (i === 3) { continue; } if (i === 8) { break; } console.log(i); } let x = 0; while (x < 10) { //if(x===3){continue;} if (x === 8) { break; } //console.log(x); x++; } //console.log(x); </script> </body> </html> Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 8. Keyboard Event Listeners - Dynamically Add Page Elements input and divs <!doctype html><html> <head> <title>Questions and Answers JavaScript</title> </head> <body> <script> const output = document.createElement('div'); const message = document.createElement('div'); const btn = document.createElement('button'); document.body.append(output); output.append(message); output.append(btn); btn.textContent = "Click to add input"; btn.style.backgroundColor = 'red'; btn.style.color = 'white'; btn.style.padding = '10px'; btn.addEventListener('click', maker) function maker() { const tempDiv = document.createElement('div'); const newInput = document.createElement('input'); Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 9. output.append(tempDiv); tempDiv.append(newInput); newInput.value = 'test'; newInput.hiddenValue = Math.random().toString(16).substr(-6); newInput.style.backgroundColor = '#' + newInput.hiddenValue; newInput.focus(); newInput.addEventListener('keyup', log); newInput.addEventListener('keypress', log); newInput.addEventListener('keydown', function (e) { console.log(e.keyCode); if (e.keyCode == 13) { message.innerHTML += `<div style="background:#${newInput.hiddenValue}">${newInput.value}</d iv>`; } }); } function log(event) { console.log(event); } </script> </body> </html> Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 10. Create Page Elements add Dynamically on the Page <!doctype html><html> <head> <title>Questions and Answers JavaScript</title> </head> <body> <script> const btn = document.createElement('button'); const output = document.createElement('div'); const message = document.createElement('div'); btn.textContent = "Click Me!"; message.textContent = "Hello World"; document.body.append(output); output.append(message); output.append(btn); btn.addEventListener('click', () => { const today = new Date(); message.textContent = `${today.getHours()} ${today.getMinutes()} ${today.getSeconds()}`; }) </script> </body> </html> Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 11. Pure JavaScript Dice - Create Elements and Build HTML for Dice <!doctype html><html> <head> <title>Questions and Answers JavaScript</title> </head> <body> <script> const diceView = [[5], [1, 9], [1, 5, 9], [1, 3, 7, 9], [1, 3, 5, 7, 9], [1, 3, 4, 6, 7, 9]]; const btn = document.createElement('button'); btn.textContent = "Roll Dice"; const playArea = document.createElement('div'); document.body.prepend(playArea); playArea.append(btn); const area1 = document.createElement('div'); const area2 = document.createElement('div'); const container = document.createElement('div'); playArea.append(container); container.append(area1); container.append(area2); area1.textContent = "first Dice"; area2.textContent = "second Dice"; addBorders(area1); Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 12. addBorders(area2); btn.addEventListener('click', () => { rollValue(); console.log(area1.val); console.log(area2.val); }) function genDice(val) { let html = '<div>'; let tempArr = diceView[val]; console.log(tempArr); for (let x = 1; x < 10; x++) { let tempVal = 'white'; if (tempArr.includes(x)) { tempVal = 'black'; } html += `<span style="width:90px;display:inline-block;height:90px;border-radius :20px;background-color:${tempVal};margin:2px;"></span>`; } html += '</div>'; return html; } function rollValue() { area1.val = Math.floor(Math.random() * 6); area2.val = Math.floor(Math.random() * 6); area1.innerHTML = genDice(area1.val); Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 13. area2.innerHTML = genDice(area2.val); } function addBorders(el) { el.style.border = '1px solid #ddd'; el.style.borderRadius = "10px"; el.style.padding = '10px'; el.style.fontSize = '1.5em'; el.style.width = '290px'; el.style.height = '290px'; el.style.margin = '10px'; el.style.backgroundColor = 'white'; //el.style.width = '40%'; el.style.float = 'left'; //el.style.height = el.offsetWidth+'px'; } </script> </body> </html> Create a JavaScript popup Modal <!doctype html><!doctype html> <html> <head> <title>Course</title> Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 14. <style> .modal { position: fixed; z-index: 5; left: 0; top: 0; width: 100%; height: 100%; background-color: rgb(0, 0, 0); background-color: rgba(0, 0, 0, 0.3); display: none; } .modal-body { background-color: white; margin: 20% auto; padding: 20px; border: 1px solid #333; border-radius: 25px; width: 70%; min-height: 200px; } .close { float: right; color: red; font-size: 2em; font-weight: bold; Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 15. } .close:hover { color: black; cursor: pointer; } </style> </head> <body> <button class='modal1'>Open 1</button> <button class='modal1'>Open 2</button> <div class="modal" id="main"> <div class="modal-body"> <span class="close">&times;</span> <div class="modal-text">Modal Text <br> test </div> </div> </div> <script> const btns = document.querySelectorAll('.modal1'); const output = document.querySelector('.modal-text'); btns.forEach((btn) => { btn.addEventListener('click', (e) => { myModal.style.display = 'block'; console.log(e.target.textContent); let val = e.target.textContent; let html = ""; switch (val) { Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 16. case 'Open 1': html = 'Number one is open <h1>ONE</h1>'; break; case 'Open 2': html = '<h1>TWO</h1>'; break; default: html = '<h1>ERROR</h1>'; } output.innerHTML = html; }) }) const closer = document.querySelector('.close'); const myModal = document.querySelector('#main'); closer.addEventListener('click', closeModal); myModal.addEventListener('click', closeModal); function closeModal() { myModal.style.display = 'none'; } </script> </body> </html> Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 17. JavaScript Request Animation Frame Simple Counter <!DOCTYPE html> <html> <head> <title>test</title> </head> <body> <h1>Hello World</h1> <script> const output = document.querySelector('h1'); output.textContent = 'Counter'; let reqVal = requestAnimationFrame(step); let start; function step(cnt) { console.log(cnt); if (start == undefined) { start = cnt; } const val = Math.floor(cnt - start); const str = String(val); console.log(str[0]); const mil = str.slice(1, 4); console.log(mil); Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 18. console.log(val); output.textContent = `${str[0]} : ${mil}`; if (val < 5000) { reqVal = requestAnimationFrame(step); } } </script> </body> </html> QuerySelector adding elements dynamically to page use of NodeList <!doctype html> <html> <head> <title>Example querySelectorAll</title> </head> <body> <ul></ul> <input type="text" name="myInput" value="test"> <button>Click Me to add item</button> <script> const ul = document.querySelector('ul'); Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 19. const li = document.querySelectorAll('li'); const myInput = document.querySelector('input[name="myInput"]'); const btn = document.querySelector('button'); let x = 0; let val = myInput.value; btn.addEventListener('click', (e) => { //console.log(e); x++; e.target.textContent = 'Clicked ' + x; addListItem(); }) function addListItem() { //console.log(myInput.value); //console.log(val); console.dir(ul); console.log(ul.children.length); console.log(ul.childElementCount); const lis = document.querySelectorAll('li'); //console.log(lis.length); if (myInput.value.length > 3 && lis.length < 5) { const li = document.createElement('li'); li.textContent = myInput.value; const val1 = ul.appendChild(li); Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 20. //console.log(val1); } } </script> </body> </html> Adding Event Listeners to All Matching Elements on Page - Dynamically adding <!doctype html> <html> <head> <title>Example querySelectorAll Click</title> <style> .active { color: blue; } </style> </head> <body> <h1>Hello</h1> <ul class="myList"> Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 21. <li>One</li> <li>Two</li> <li>Three</li> </ul> <script> const ul = document.querySelector('ul.myList'); const lis = ul.querySelectorAll('li'); const btn = document.createElement('button'); let counter = lis.length; btn.textContent = 'Click Me'; document.body.append(btn); btn.addEventListener('click', (e) => { counter++; const li = document.createElement('li'); li.acter = 0; li.textContent = `Value (${counter}) ${li.acter} - `; li.addEventListener('click', updateItem); ul.append(li); }) lis.forEach((li) => { console.log(li); li.acter = 0; li.addEventListener('click', updateItem); }) function updateItem(e) { Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 22. const ele = e.target; console.dir(ele); ele.acter++; console.log(ele.acter); let temp = ele.textContent; ele.textContent = `${temp} ${ele.acter}`; ele.classList.toggle('active'); console.log(ele.classList.contains('active')); } </script> </body> </html> Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/