diff --git a/polls/static/polls/js/detail.js b/polls/static/polls/js/detail.js new file mode 100644 index 0000000..86e7e5a --- /dev/null +++ b/polls/static/polls/js/detail.js @@ -0,0 +1,38 @@ +const toggleChoice = (button, choiceId) => { + const choiceInput = document.querySelector(`input[name="choice"][value="${choiceId}"]`); + + if (choiceInput) { + // already selected -> unselect it + if (choiceInput.checked) { + choiceInput.checked = false; + button.classList.remove('bg-green-500', 'border-solid', 'border-2', 'border-black', 'hover:bg-green-600'); + button.classList.add('bg-white', 'border-solid', 'border-2', 'border-black', 'hover:bg-white'); + // Clear display + document.getElementById('selected-choice').textContent = 'Please Select a Choiceπ'; + } else { + // Unselect all choices + document.querySelectorAll('input[name="choice"]').forEach((choice) => { + choice.checked = false; + }); + + // Select the clicked choice + choiceInput.checked = true; + + // Reset the style of all choice buttons + document.querySelectorAll('.choice-button').forEach((btn) => { + btn.classList.remove('bg-green-500', 'border-solid', 'border-2', 'border-black', 'hover:bg-green-600'); + btn.classList.add('bg-white', 'border-solid', 'border-2', 'border-black', 'hover:bg-white'); + }); + + button.classList.remove('bg-white', 'border-solid', 'border-2', 'border-black', 'hover:bg-white'); + button.classList.add('bg-green-500', 'border-solid', 'border-2', 'border-black', 'hover:bg-green-600'); + + const choiceText = button.textContent.trim(); + document.getElementById('selected-choice').textContent = `You select: ${choiceText}`; + } + + // Enable the "Vote" button -> if select + const voteButton = document.getElementById('vote-button'); + voteButton.disabled = !document.querySelector('input[name="choice"]:checked'); + } +}; \ No newline at end of file diff --git a/polls/templates/polls/base.html b/polls/templates/polls/base.html index 7987a04..fc6124b 100644 --- a/polls/templates/polls/base.html +++ b/polls/templates/polls/base.html @@ -2,16 +2,20 @@ -
- - - - - -