Simple Poll

Developer
Size
2,159 Kb
Views
10,120

How do I make an simple poll?

What is a simple poll? How do you make a simple poll? This script and codes were developed by Laura Kelly on 29 January 2023, Sunday.

Simple Poll Previews

Simple Poll - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Simple Poll</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <div id="choices"> <div class="choice"> <div class="result"> <div class="bar" data-choice="1"></div> </div> <div class="label"> <p>Choice 1</p> <button data-choice="1">Vote</button> </div> </div> <div class="choice"> <div class="result"> <div class="bar" data-choice="2"></div> </div> <div class="label"> <p>Choice 2</p> <button data-choice="2">Vote</button> </div> </div> <div class="choice"> <div class="result"> <div class="bar" data-choice="3"></div> </div> <div class="label"> <p>Choice 3</p> <button data-choice="3">Vote</button> </div> </div>
</div> <script src="js/index.js"></script>
</body>
</html>

Simple Poll - Script Codes CSS Codes

.choice { height: 400px; display: inline-block; width: 100px;
}
.result { height: 330px; display: flex; flex-direction: column; justify-content: flex-end;
}
.bar { height: 0%; width: 70%; margin: 0 auto;
}
.bar[data-choice="1"] { background-color: red;
}
.bar[data-choice="2"] { background-color: green;
}
.bar[data-choice="3"] { background-color: blue;
}
.label { height: 70px; text-align: center;
}

Simple Poll - Script Codes JS Codes

var buttons = document.querySelectorAll(".choice button"), tally = { 1: 0, 2: 0, 3: 0, total: 0 };
function vote(choice) { tally[choice]++; tally["total"]++; console.log(tally);
}
function barPercentage(node, tally) { var choice = node.dataset.choice; if (tally[choice]) return tally[choice]/tally["total"] * 100; return 0;
}
function renderBars() { var bars = document.getElementsByClassName("bar"); for (var i = 0; i < bars.length; i++) { var percentage = barPercentage(bars[i], tally); console.log(percentage) bars[i].style.height = percentage.toString() + "%"; }
}
function setup() { // Set up event listeners for (var i = 0; i < buttons.length; i++) { buttons[i].addEventListener("click", function(e) { vote(e.target.dataset["choice"]); renderBars(); }); } renderBars();
}
setup();
Simple Poll - Script Codes
Simple Poll - Script Codes
Home Page Home
Developer Laura Kelly
Username laurakelly
Uploaded January 29, 2023
Rating 3
Size 2,159 Kb
Views 10,120
Do you need developer help for Simple Poll?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Laura Kelly (laurakelly) Script Codes
Create amazing captions with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!