Skip to content

Commit

Permalink
Day 7 - Event Handling
Browse files Browse the repository at this point in the history
  • Loading branch information
IshanviChauhan committed Feb 20, 2025
1 parent 71de65b commit d2e64e9
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 0 deletions.
74 changes: 74 additions & 0 deletions Day7/eventStyle.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
*{
margin: 0;
padding: 0;
box-sizing: border-box;
font-family:'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
body{
padding: 1rem;
transition: .4s;
display: flex;
flex-direction: column;
}
header{
display: flex;
justify-content: center;
margin: 30px 0 30px 0;
}
header button{
padding: 15px;
border-radius: 1rem;
border: none;
background-color: #e9e7ea;
font-size: 15px;
font-weight: bold;
cursor: pointer;
}

#parent{
display: flex;
flex-wrap: wrap;
}
h1{
color: white;
text-align: center;
margin: 10px 0 10px 0;
text-shadow: 0 0 30px black;
}
h4{
color: #703888;
font-size: 20px;
font-weight: bold;
text-align: center;
}
h6{
color: #1a5188;
font-size: 18px;
font-weight: bold;
text-align: center;
}
p{
color: #149054;
font-size: 16px;
font-weight: bold;
text-align: center;
}
.card{
border-radius: 1rem;
box-shadow: 4px 4px 4px rgb(238, 237, 172);
width: 200px;
background-color: rgb(255, 254, 211);
margin: 1rem auto;
padding: 20px;
transition: .4s;
}
.card:hover{
box-shadow: 0 0 20px rgb(192, 191, 110);
flex-grow: .3;
cursor: pointer;
}
.card p{
padding: 10px;
margin: 10px;
transition: .4s;
}
47 changes: 47 additions & 0 deletions Day7/events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const data = [
{ name: 'Ishanvi', email: '[email protected]', city: 'Ghaziabad'},
{ name: 'Kshitiz', email: '[email protected]', city: 'Delhi'},
{ name: 'Asmit', email: '[email protected]', city: 'Kanpur'},
{ name: 'Arpit', email: '[email protected]', city: 'Jalaun'},
{ name: 'Anisha', email: '[email protected]', city: 'Ghaziabad'}
];

const root = document.getElementById('parent');

data.forEach((elem) => {
const newCard = document.createElement('div');
newCard.addEventListener("click", () => {
newCard.style.backgroundColor = getRandomColor();
});
//setAttribute
//classList.add
newCard.className = 'card';
newCard.innerHTML = `
<h4>${elem.name}</h4>
<h6>${elem.email}</h6>
<p class='text'>${elem.city}</p>
`;
root.appendChild(newCard);
})

const getRandomColor = () => {
const randomRed = Math.floor(Math.random() * 255);
const randomGreen = Math.floor(Math.random() * 255);
const randomBlue = Math.floor(Math.random() * 255);
return `rgb(${randomRed}, ${randomGreen}, ${randomBlue})`;
}

const body = document.querySelector('body');
const handleBgChange = () => {
console.log("Button Clicked");
//Logic to change background color
body.style.backgroundColor = getRandomColor();;
}

const textElement = document.querySelectorAll('.text');
textElement.forEach((p) => {
p.addEventListener('click', (event) => {
event.stopPropagation();
p.style.backgroundColor = getRandomColor();
},false);
});
18 changes: 18 additions & 0 deletions Day7/form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form Handling</title>
</head>
<body>
<form action="">
<input type="text"
onchange="handleTextChange(event)"
onkeydown="handleKeyDown(event)"
onkeyup="handleKeyUp(event)">
</form>

<script src="formEvent.js"></script>
</body>
</html>
12 changes: 12 additions & 0 deletions Day7/formEvent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const handleTextChange = (e) => {
console.log(e);
console.log("Change:",e.target.value);
};
const handleKeyDown = (e) => {
console.log(e);
console.log("Key Down:",e.target.value);
};
const handleKeyUp = (e) => {
console.log(e);
console.log("Key Up:",e.target.value);
};
Empty file added Day7/formStyle.css
Empty file.
25 changes: 25 additions & 0 deletions Day7/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="eventStyle.css">
</head>
<body>
<h1>Events in JS</h1>
<header>
<button onclick="handleBgChange()">Change Background</button>
</header>
<div id="parent">
<!--
<div class="card">
<h4>Name</h4>
<h6>Email</h6>
<p>City</p>
</div>
-->
</div>
<script src="events.js"></script>
</body>
</html>

0 comments on commit d2e64e9

Please sign in to comment.