screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>One Checkbox Selected</title> </head> <body> <h2>Javascript Select One Checkbox</h2> <input type="checkbox" value="apple" name="option" id="option1"> Apple <br> <input type="checkbox" value='banana' name="option" id="option2"> Banana <br> <input type="checkbox" value="grapes" name="option" id="option3"> Grapes <br> <p id="result"></p> <script> const checkboxes = document.querySelectorAll('input[type="checkbox"]'); checkboxes.forEach(checkbox => { checkbox.addEventListener('change', function () { checkboxes.forEach(cb => { if (cb !== this) { cb.checked = false; } }); const result = document.getElementById('result') result.innerHTML = `Selected Item: ${this.checked ? this.value : 'No Item Selected'}` ; }); }); </script> </body> </html>