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

PPTX
Local SQLite Database with Node for beginners
PDF
Chrome DevTools Introduction 2020 Web Developers Guide
PDF
Javascript projects Course
PDF
JavaScript Jump Start 20220214
PPTX
JavaScript DOM - Dynamic interactive Code
PDF
Node.js Crash Course (Jump Start)
PPTX
Monster JavaScript Course - 50+ projects and applications
PPTX
Introduction to java_script
Local SQLite Database with Node for beginners
Chrome DevTools Introduction 2020 Web Developers Guide
Javascript projects Course
JavaScript Jump Start 20220214
JavaScript DOM - Dynamic interactive Code
Node.js Crash Course (Jump Start)
Monster JavaScript Course - 50+ projects and applications
Introduction to java_script

What's hot (20)

PDF
JAVA SCRIPT
PPTX
Java script
PDF
Why and How to Use Virtual DOM
PPT
Java script
PDF
22 j query1
PDF
How to make Ajax work for you
PDF
Develop High Performance Windows 8 Application with HTML5 and JavaScriptHigh ...
PDF
Web Projects: From Theory To Practice
PDF
Data presentation with dust js technologies backing linkedin
PDF
Web Components
PDF
1 ppt-ajax with-j_query
PDF
Front End Development: The Important Parts
PDF
Difference between java script and jquery
PPT
J query b_dotnet_ug_meet_12_may_2012
PDF
Introduction to web components
PPTX
Mdst 3559-02-10-jquery
PPT
Web Performance Tips
PPTX
JavaScript and jQuery Basics
PDF
JavaScript and BOM events
PDF
Better Selenium Tests with Geb - Selenium Conf 2014
JAVA SCRIPT
Java script
Why and How to Use Virtual DOM
Java script
22 j query1
How to make Ajax work for you
Develop High Performance Windows 8 Application with HTML5 and JavaScriptHigh ...
Web Projects: From Theory To Practice
Data presentation with dust js technologies backing linkedin
Web Components
1 ppt-ajax with-j_query
Front End Development: The Important Parts
Difference between java script and jquery
J query b_dotnet_ug_meet_12_may_2012
Introduction to web components
Mdst 3559-02-10-jquery
Web Performance Tips
JavaScript and jQuery Basics
JavaScript and BOM events
Better Selenium Tests with Geb - Selenium Conf 2014
Ad

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

PPTX
lec 14-15 Jquery_All About J-query_.pptx
PPT
Java script Learn Easy
PDF
Ajax Performance Tuning and Best Practices
PDF
Meta Programming with JavaScript
PDF
20190118_NetadashiMeetup#8_React2019
PPTX
PPTX
JavaScript front end performance optimizations
PDF
CSS framework By Palash
PDF
Webpack packing it all
PDF
Packing it all: JavaScript module bundling from 2000 to now
PPTX
Coldfusion with Keith Diehl
PPT
Please dont touch-3.6-jsday
PDF
Scalable vector ember
PDF
Building iPhone Web Apps using "classic" Domino
PDF
TDC2017 | Florianopolis - Trilha DevOps How we figured out we had a SRE team ...
PDF
[Bristol WordPress] Supercharging WordPress Development
PDF
What you need to know bout html5
PPTX
PDF
Wordless, stop writing WordPress themes like it's 1998
PPTX
Javascript first-class citizenery
lec 14-15 Jquery_All About J-query_.pptx
Java script Learn Easy
Ajax Performance Tuning and Best Practices
Meta Programming with JavaScript
20190118_NetadashiMeetup#8_React2019
JavaScript front end performance optimizations
CSS framework By Palash
Webpack packing it all
Packing it all: JavaScript module bundling from 2000 to now
Coldfusion with Keith Diehl
Please dont touch-3.6-jsday
Scalable vector ember
Building iPhone Web Apps using "classic" Domino
TDC2017 | Florianopolis - Trilha DevOps How we figured out we had a SRE team ...
[Bristol WordPress] Supercharging WordPress Development
What you need to know bout html5
Wordless, stop writing WordPress themes like it's 1998
Javascript first-class citizenery
Ad

More from Laurence Svekis ✔ (19)

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

Recently uploaded (20)

PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PDF
Skin Care and Cosmetic Ingredients Dictionary ( PDFDrive ).pdf
PPTX
B.Sc. DS Unit 2 Software Engineering.pptx
PDF
Mucosal Drug Delivery system_NDDS_BPHARMACY__SEM VII_PCI.pdf
PDF
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
PPTX
Virtual and Augmented Reality in Current Scenario
PDF
Race Reva University – Shaping Future Leaders in Artificial Intelligence
PDF
Empowerment Technology for Senior High School Guide
PDF
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
PPTX
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
PDF
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
PPTX
What’s under the hood: Parsing standardized learning content for AI
PDF
Environmental Education MCQ BD2EE - Share Source.pdf
PDF
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
PPTX
Share_Module_2_Power_conflict_and_negotiation.pptx
PDF
FORM 1 BIOLOGY MIND MAPS and their schemes
PDF
What if we spent less time fighting change, and more time building what’s rig...
PDF
MICROENCAPSULATION_NDDS_BPHARMACY__SEM VII_PCI .pdf
DOCX
Cambridge-Practice-Tests-for-IELTS-12.docx
Paper A Mock Exam 9_ Attempt review.pdf.
Skin Care and Cosmetic Ingredients Dictionary ( PDFDrive ).pdf
B.Sc. DS Unit 2 Software Engineering.pptx
Mucosal Drug Delivery system_NDDS_BPHARMACY__SEM VII_PCI.pdf
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
Virtual and Augmented Reality in Current Scenario
Race Reva University – Shaping Future Leaders in Artificial Intelligence
Empowerment Technology for Senior High School Guide
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
What’s under the hood: Parsing standardized learning content for AI
Environmental Education MCQ BD2EE - Share Source.pdf
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
Share_Module_2_Power_conflict_and_negotiation.pptx
FORM 1 BIOLOGY MIND MAPS and their schemes
What if we spent less time fighting change, and more time building what’s rig...
MICROENCAPSULATION_NDDS_BPHARMACY__SEM VII_PCI .pdf
Cambridge-Practice-Tests-for-IELTS-12.docx

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/