Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

50Projects-HTML-CSS-JavaScript : Interactive polling app #53

Merged
merged 2 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,17 @@ In order to run this project you need:
</details>
</li>

<li>
<details>
<summary>Simple Chat App</summary>
<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>
<ul>
<li><a href="https://tajulafreen.github.io/50Projects-HTML-CSS-JavaScript/Source-Code/InteractivePolling/">Live Demo</a></li>
<li><a href="https://github.com/tajulafreen/50Projects-HTML-CSS-JavaScript/tree/main/Source-Code/InteractivePolling">Source</a></li>
</ul>
</details>
</li>

</ol>

<p align="right">(<a href="#readme-top">back to top</a>)</p>
Expand Down
29 changes: 29 additions & 0 deletions Source-Code/InteractivePolling/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Interactive Polling App</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="app">
<h1>Vote for Your Favorite Programming Language</h1>
<div class="poll-options">
<button class="poll-option" data-vote="Ruby">Ruby</button>
<button class="poll-option" data-vote="Python">Python</button>
<button class="poll-option" data-vote="Java">Java</button>
<button class="poll-option" data-vote="Javascript">Javascript</button>
</div>
<div class="poll-results">
<h2>Results</h2>
<ul id="results-list">
<!-- Poll results will be displayed here -->
</ul>
</div>
</div>

<!-- JavaScript File -->
<script src="script.js"></script>
</body>
</html>
48 changes: 48 additions & 0 deletions Source-Code/InteractivePolling/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
document.addEventListener('DOMContentLoaded', () => {
const pollOptions = document.querySelectorAll('.poll-option');
const resultsList = document.getElementById('results-list');

const votes = JSON.parse(localStorage.getItem('votes')) || {
Ruby: 0,
Python: 0,
Java: 0,
Javascript: 0,
};

// Update the results on the page
function updateResults() {
resultsList.innerHTML = ''; // Clear previous results

const totalVotes = Object.values(votes).reduce(
(total, count) => total + count,
0,
);

// Display the updated results
Object.entries(votes).forEach(([option, count]) => {
const percentage = totalVotes
? ((count / totalVotes) * 100).toFixed(1)
: 0;

const resultItem = document.createElement('li');
resultItem.innerHTML = `
<strong>${option}</strong>: ${count} votes (${percentage}%)
<div class="bar" style="width: ${percentage}%;"></div>
`;
resultsList.appendChild(resultItem);
});
}

// Display initial poll results
updateResults();

// Event listener for voting
pollOptions.forEach((option) => {
option.addEventListener('click', () => {
const selectedVote = option.getAttribute('data-vote');
votes[selectedVote] += 1;
localStorage.setItem('votes', JSON.stringify(votes));
updateResults();
});
});
});
67 changes: 67 additions & 0 deletions Source-Code/InteractivePolling/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background-color: #f9f9f9;
}

.poll-container {
background: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
width: 100%;
max-width: 400px;
text-align: center;
}

.poll-question {
font-size: 1.2rem;
margin-bottom: 20px;
}

label {
display: block;
margin: 10px 0;
font-size: 1rem;
}

button {
margin-top: 20px;
padding: 10px 20px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 4px;
cursor: pointer;
}

button:hover {
background-color: #0056b3;
}

.poll-results {
margin-top: 20px;
text-align: left;
}

.poll-results ul {
list-style: none;
padding: 0;
}

.poll-results li {
padding: 5px 0;
font-size: 1rem;
}

.poll-results .bar {
background-color: #007bff;
height: 10px;
border-radius: 4px;
margin-top: 5px;
}