Skip to content

Commit 499dba1

Browse files
authored
Merge pull request #53 from tajulafreen/Interactive_Polling_App
50Projects-HTML-CSS-JavaScript : Interactive polling app
2 parents 8da7c28 + 4fb1b8e commit 499dba1

File tree

4 files changed

+155
-0
lines changed

4 files changed

+155
-0
lines changed

Diff for: README.md

+11
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,17 @@ In order to run this project you need:
562562
</details>
563563
</li>
564564

565+
<li>
566+
<details>
567+
<summary>Simple Chat App</summary>
568+
<p>This is an interactive polling app that allows users to vote on a specific question. Users can vote for their preferred options and view the results in real-time. The app tracks the votes for each option and stores them in the local storage so the votes persist even after the page is refreshed.</p>
569+
<ul>
570+
<li><a href="https://tajulafreen.github.io/50Projects-HTML-CSS-JavaScript/Source-Code/InteractivePolling/">Live Demo</a></li>
571+
<li><a href="https://github.com/tajulafreen/50Projects-HTML-CSS-JavaScript/tree/main/Source-Code/InteractivePolling">Source</a></li>
572+
</ul>
573+
</details>
574+
</li>
575+
565576
</ol>
566577

567578
<p align="right">(<a href="#readme-top">back to top</a>)</p>

Diff for: Source-Code/InteractivePolling/index.html

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Interactive Polling App</title>
7+
<link rel="stylesheet" href="style.css">
8+
</head>
9+
<body>
10+
<div class="app">
11+
<h1>Vote for Your Favorite Programming Language</h1>
12+
<div class="poll-options">
13+
<button class="poll-option" data-vote="Ruby">Ruby</button>
14+
<button class="poll-option" data-vote="Python">Python</button>
15+
<button class="poll-option" data-vote="Java">Java</button>
16+
<button class="poll-option" data-vote="Javascript">Javascript</button>
17+
</div>
18+
<div class="poll-results">
19+
<h2>Results</h2>
20+
<ul id="results-list">
21+
<!-- Poll results will be displayed here -->
22+
</ul>
23+
</div>
24+
</div>
25+
26+
<!-- JavaScript File -->
27+
<script src="script.js"></script>
28+
</body>
29+
</html>

Diff for: Source-Code/InteractivePolling/script.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
document.addEventListener('DOMContentLoaded', () => {
2+
const pollOptions = document.querySelectorAll('.poll-option');
3+
const resultsList = document.getElementById('results-list');
4+
5+
const votes = JSON.parse(localStorage.getItem('votes')) || {
6+
Ruby: 0,
7+
Python: 0,
8+
Java: 0,
9+
Javascript: 0,
10+
};
11+
12+
// Update the results on the page
13+
function updateResults() {
14+
resultsList.innerHTML = ''; // Clear previous results
15+
16+
const totalVotes = Object.values(votes).reduce(
17+
(total, count) => total + count,
18+
0,
19+
);
20+
21+
// Display the updated results
22+
Object.entries(votes).forEach(([option, count]) => {
23+
const percentage = totalVotes
24+
? ((count / totalVotes) * 100).toFixed(1)
25+
: 0;
26+
27+
const resultItem = document.createElement('li');
28+
resultItem.innerHTML = `
29+
<strong>${option}</strong>: ${count} votes (${percentage}%)
30+
<div class="bar" style="width: ${percentage}%;"></div>
31+
`;
32+
resultsList.appendChild(resultItem);
33+
});
34+
}
35+
36+
// Display initial poll results
37+
updateResults();
38+
39+
// Event listener for voting
40+
pollOptions.forEach((option) => {
41+
option.addEventListener('click', () => {
42+
const selectedVote = option.getAttribute('data-vote');
43+
votes[selectedVote] += 1;
44+
localStorage.setItem('votes', JSON.stringify(votes));
45+
updateResults();
46+
});
47+
});
48+
});

Diff for: Source-Code/InteractivePolling/style.css

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
body {
2+
font-family: Arial, sans-serif;
3+
margin: 0;
4+
padding: 0;
5+
display: flex;
6+
justify-content: center;
7+
align-items: center;
8+
min-height: 100vh;
9+
background-color: #f9f9f9;
10+
}
11+
12+
.poll-container {
13+
background: #fff;
14+
padding: 20px;
15+
border-radius: 8px;
16+
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
17+
width: 100%;
18+
max-width: 400px;
19+
text-align: center;
20+
}
21+
22+
.poll-question {
23+
font-size: 1.2rem;
24+
margin-bottom: 20px;
25+
}
26+
27+
label {
28+
display: block;
29+
margin: 10px 0;
30+
font-size: 1rem;
31+
}
32+
33+
button {
34+
margin-top: 20px;
35+
padding: 10px 20px;
36+
background-color: #007bff;
37+
color: #fff;
38+
border: none;
39+
border-radius: 4px;
40+
cursor: pointer;
41+
}
42+
43+
button:hover {
44+
background-color: #0056b3;
45+
}
46+
47+
.poll-results {
48+
margin-top: 20px;
49+
text-align: left;
50+
}
51+
52+
.poll-results ul {
53+
list-style: none;
54+
padding: 0;
55+
}
56+
57+
.poll-results li {
58+
padding: 5px 0;
59+
font-size: 1rem;
60+
}
61+
62+
.poll-results .bar {
63+
background-color: #007bff;
64+
height: 10px;
65+
border-radius: 4px;
66+
margin-top: 5px;
67+
}

0 commit comments

Comments
 (0)