Build me an HTML Website where I can vote on text thumbs up and thumbs down. The result…
- User
Build me an HTML Website where I can vote on text thumbs up and thumbs down. The result should be saved by javascript
Assistant
Below is a simple example of an HTML website where you can vote on text with thumbs up and thumbs down. The results are saved using JavaScript and stored in the browser's local storage.
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Voting Website</title> <style> .vote-container { width: 50%; margin: 40px auto; text-align: center; } .vote-text { font-size: 24px; margin-bottom: 20px; } .vote-buttons { margin-top: 20px; } .vote-buttons button { margin: 0 10px; padding: 10px 20px; font-size: 18px; cursor: pointer; } .vote-result { margin-top: 20px; font-size: 18px; } </style> </head> <body> <div class="vote-container"> <div class="vote-text" id="vote-text">This is a sample text to vote on.</div> <div class="vote-buttons"> <button id="thumbs-up">Thumbs Up</button> <button id="thumbs-down">Thumbs Down</button> </div> <div class="vote-result"> <span id="up-votes">Up votes: 0</span> <span id="down-votes"> | Down votes: 0</span> </div> </div> <script src="script.js"></script> </body> </html>script.js
// Get elements const voteText = document.getElementById('vote-text'); const thumbsUpButton = document.getElementById('thumbs-up'); const thumbsDownButton = document.getElementById('thumbs-down'); const upVotesSpan = document.getElementById('up-votes'); const downVotesSpan = document.getElementById('down-votes'); // Initialize votes let upVotes = 0; let downVotes = 0; // Load existing votes from local storage if (localStorage.getItem('upVotes')) { upVotes = parseInt(localStorage.getItem('upVotes')); upVotesSpan.textContent = `Up votes: ${upVotes}`; } if (localStorage.getItem('downVotes')) { downVotes = parseInt(localStorage.getItem('downVotes')); downVotesSpan.textContent = `Down votes: ${downVotes}`; } // Add event listeners to buttons thumbsUpButton.addEventListener('click', () => { upVotes++; upVotesSpan.textContent = `Up votes: ${upVotes}`; saveVotes(); }); thumbsDownButton.addEventListener('click', () => { downVotes++; downVotesSpan.textContent = `Down votes: ${downVotes}`; saveVotes(); }); // Function to save votes to local storage function saveVotes() { localStorage.setItem('upVotes', upVotes); localStorage.setItem('downVotes', downVotes); }How it works:
- The HTML file creates a simple webpage with a text to vote on, two buttons for thumbs up and thumbs down, and a section to display the vote results.
- The JavaScript file gets references to the HTML elements and initializes the vote counts to 0.
- It then checks if there are existing votes in the browser's local storage and updates the vote counts and display accordingly.
- When a button is clicked, the corresponding vote count is incremented, the display is updated, and the new vote counts are saved to local storage.
- The
saveVotesfunction is used to save the current vote counts to local storage.
Note: This code uses the browser's local storage to save the votes, which means that the votes will only be stored on the current browser and device. If you want to store the votes on a server, you would need to use a server-side programming language like PHP or Node.js and a database.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.