Replicating radio button functionality using buttons in an HTML document

I am attempting to simulate radio button behavior using normal buttons for a quiz application. Currently, my code is working as intended and permanently highlights buttons with the desired color. However, I want all other buttons in a question to be white, and only the selected one should display the desired color. This is what I am attempting to achieve:

var count = 1;

function setColor(btn, color) {
  var property = document.getElementById(btn);
  if (count == 0) {
    property.style.backgroundColor = color;
    count = 1;
  } else {
    property.style.backgroundColor = color;
    count = 0;
  }
}
.button {
  border: 2px solid black;
  padding: 16px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 5vw;
  margin: 4px 2px;
  transition-duration: 0.4s;
  cursor: pointer;
  width: 100%;
}

.div1 {
  width: 95vw;
  border-radius: 20px;
  background-color: #ddd;
  padding: 30px;
  margin: 0 auto;
}

.card {
  /* Add shadows to create the "card" effect */
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.9);
  transition: 0.3s;
}

/* On mouse-over, add a deeper shadow */
.card:hover {
  box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.9);
}

/* Add some padding inside the card container */
.container {
  padding: 2px 16px;
}

h1 {
  font-size: 6vw;
}

input {
  width: 100%;
  height: 50px;
  font-size: 5vw;
}

select {
  width: 100%;
  height: 50px;
  font-size: 5vw;
}

label {
  width: 100%;
  height: 40px;
  font-size: 4vw;
}
<div class="card div1">
  <label for="Q1">Question 1 ?</label>
  <button id="button11" class="button" onclick="setColor('button11', 'red')">YES</button>
  <br>
  <button id="button12" class="button" onclick="setColor('button12', 'green')">NO</button>
  <br>
  <button id="button13" class="button" onclick="setColor('button13', 'blue')">N/A</button>
</div>
<br>
<div class="card div1">
  <label for="Q2">Question 2 ?</label>
  <button id="button21" class="button" onclick="setColor('button21', 'red')">YES</button>
  <br>
  <button id="button22" class="button" onclick="setColor('button22', 'green')">NO</button>
  <br>
  <button id="button23" class="button" onclick="setColor('button23', 'blue')">N/A</button>
</div>
<br>
<div class="card div1">
  <label for="Q3">Question 3 ?</label>
  <button id="button31" class="button" onclick="setColor('button31', 'red')">YES</button>
  <br>
  <button id="button32" class="button" onclick="setColor('button32', 'green')">NO</button>
  <br>
  <button id="button33" class="button" onclick="setColor('button33', 'blue')">N/A</button>
</div>

Progress so far

Answer №1

My success with utilizing radio buttons is evident in the following code snippet:

<html>

<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<style>
body {
    font-family:sans-serif;
}

#radioset {
    margin:4px;
   
    float:center;
    
  
}

#radioset label {
    float:left;
    width:170px;
    margin:4px;
    background-color:#EFEFEF;
    border-radius:4px;
    border:1px solid #D0D0D0;
    overflow:auto;
    width:100%
       
}

#radioset label span {
    text-align:center;
    font-size: 32px;
    padding:13px 0px;
    display:block;
    
}

#radioset label input {
    position:absolute;
    top:-20px;
    visibility: hidden;
}

#radioset input:checked + span {
    background-color:red;
    color:#F7F7F7;
}

.button {
  /*background-color: #4CAF50;  Green */
  border: 2px solid black;
  /*color: white;*/
  padding: 16px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 5vw;
  margin: 4px 2px;
  transition-duration: 0.4s;
  cursor: pointer;
  width: 100%;
}

.div1 {
  width: 95vw;
  
  border-radius: 20px;
  background-color: #ddd;
  padding: 10px;
  margin:0 auto;
  overflow:hidden;
}


.card {
  /* Add shadows to create the "card" effect */
  box-shadow: 0 4px 8px 0 rgba(0,0,0,0.9);
  transition: 0.3s;
}

/* On mouse-over, add a deeper shadow */
.card:hover {
  box-shadow: 0 8px 16px 0 rgba(0,0,0,0.9);
}



input{
width: 100%;
height: 50px;
font-size: 5vw;
}


h1 {
    font-size:6vw;
}

</style>
</head>

<body>

<!-- QUESTIONS-- -->
<div class="div1 card">
<h1>Question 1 ?</h1>

<div id="radioset">

    <label><input type="radio" name="toggle"><span>YES</span></label><br>
    <label><input type="radio" name="toggle"><span>NO</span></label><br>
    <label><input type="radio" name="toggle"><span>N/A</span></label><br>
    
</div>
</div>


<br>
<div class="div1 card">
<h1>Question 2 ?</h1>

<div id="radioset">

    <label ><input type="radio" name="toggle2"><span>YES</span></label><br>
    <label ><input type="radio" name="toggle2"><span>NO</span></label><br>
    <label><input type="radio" name="toggle2"><span>N/A</span></label><br>
    
</div>
</div>

<br>
<div class="div1 card">
<h1>Question 3 ?</h1>

<div id="radioset">

    <label ><input type="radio" name="toggle3"><span>YES</span></label><br>
    <label ><input type="radio" name="toggle3"><span>NO</span></label><br>
    <label><input type="radio" name="toggle3"><span>N/A</span></label><br>
    
</div>
</div>


<p>___________________________________________________________________________________</p>

</body>
</html>

https://jsfiddle.net/mrgyk3zs/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The page continues to refresh even after the fetch() method is called and the promise is resolved, despite setting e.preventDefault()

Greetings! I am currently in the process of creating a basic API using ytdl and express. Specifically, the route I am focusing on is responsible for downloading a file. app.post('/audio', (req, res) => { console.log(`Initiating audio down ...

Transform the checkbox selection into an active hyperlink upon being selected

Currently, I am in the process of designing a form that allows visitors to select items they would like to view by checking relevant checkboxes. Upon clicking a Request button, the URLs of the selected items are displayed. I aim to have the URL appear as ...

Best practice for updating Form.Control text using a custom onChange method

Today, I observed a unique behavior while utilizing a custom onChange in a Form.Control. The text in the field no longer updates when a file is selected. I thoroughly checked the Git documentation HERE, but unfortunately, it does not provide information on ...

trouble with react-table's default height and flexible design

I am facing an issue with my React table configuration. I have set up scrollable columns and header following a similar example here: https://codesandbox.io/s/kowzlm5jp7?file=/index.js:281-310 In the table styles, I have specified a height like this: ...

Steps for creating a border around a container Div:1. Set the width and

Seeking assistance in bordering a div with 4 fa-icons inside. The parent div is named Container, and as a result, the border is creating excessive padding on the left and right sides horizontally. Attempted nesting bootstrap grids without success. Can anyo ...

Ending a timer from a different function in a React component

Currently, I am delving into the world of React and attempting to create a timer component. While I have successfully implemented the start function for my timer, I am now faced with the challenge of stopping the timer using an onclick handler from another ...

Check if a specific number appears exactly once in an array and output either True or False

I am facing a challenge with comparing two arrays Array1 = [1,1,1,2,2,2,3,3] Array2 =[1,1,2,1] When comparing both arrays, the desired result is True if the number of occurrences of Integer 1 are the same. Array2 = [1,1,2] //Expecting False For the ab ...

Instructions on utilizing sockets for transmitting data from javascript to python

How can I establish communication between my Node.js code and Python using sockets? In a nutshell, here is what I am looking for: Node.js: sendInformation(information) Python: receiveInformation() sendNewInformation() Node.js: receiveNewInformation( ...

Bootstrap offers an improved method for arranging elements in columns and rows, especially when you need to omit certain ones

Is there a more efficient method for aligning elements using Bootstrap? Here is my current approach: <div className="row"> <div className="col-3 font-weight-bold"> Item Name ...

Enlarging an image on canvas and creating a repeating pattern

My canvas size is 500px x 500px. I have a png image that matches the canvas size, which you can view here. I am looking to resize the image to 100px x 100px and then use it as part of a repeat pattern on the canvas. This is how I achieve this: // First d ...

When using props.onChange(e.target.value) in a textField component in Material UI, it unexpectedly returns an object instead of a value

function FormInput(props) { const classes = formInputStyles(); return ( <div> <TextField onChange={(e) => props.onChange(e.target.value)} InputProps={{ classes, disableUnderline: true }} {...pro ...

Modifying Font Style in Angular 4 Material

Is there a way to change the font in Angular 4 Material material.angular.io using the styles file in .CSS format instead of SASS? While the documentation offers an example for SASS at: https://github.com/angular/material2/blob/master/guides/typography.md ...

Trouble accessing nested components in Angular CLI beyond the first level of components

I'm diving into Angular CLI for the first time and trying to recreate a previous web project of mine. I've managed to nest and display components inside the root component successfully, but after that, I'm facing issues referencing any compo ...

Change the language of the website using JavaScript/JSON

Creating a website and aiming to utilize JavaScript for the following: If the browser language is set to French, retrieve the text from languageFr.json, otherwise, retrieve the text from languageEn.json. Here is a snippet of the code I have so far: JSON ...

Setting the cache to false during an httpget request in an mvc4 controller action: tips and tricks

My httpget request to a controller action looks like this: $.get('/Course/ExplanationCorrect/', postData, function (data) { $('#SurveyDiv').html(data); }); While it works on all other browsers, I'm facing an issue with IE10 o ...

Listening to LayersControl.Overlay checkbox clicks in React-Leaflet: A guide

My goal is to dynamically render overlay controls and bind a click event listener to the checkbox of each control in React. However, I am unsure how to provide a React ref to LayersControl or an onClick handler to LayersControl.Overlay. Is there a more eff ...

Exploring the use of color in text embellishments

Is it possible to change the color of the underline on text when hovering, while keeping it different from the text color? I have successfully achieved this in Firefox using the property "-moz-text-decoration-color". However, this does not work in other m ...

When Python / Django's render_to_string function is used, the output is rendered as text containing HTML entities

A task I am working on involves migrating a Python/Django application from: Django==1.5.1 Python version 2.6.6 To: Django==3.2 Python version 3.6.8 One issue I am encountering is with a section of code that generates an HTML template as a string and add ...

The Angular @HostListener beforeunload Event is a powerful way to handle

I've implemented the following code snippet in my main app.component.ts file within my Angular 17 project: @HostListener("window:beforeunload", ["$event"]) onTabClose($event: BeforeUnloadEvent) { $event.preventDefault(); ...

Invalid controller function path specified in Laravel framework for AJAX request

I am struggling to find the correct ajax URL as the one I am currently using is not working. The console shows the following error message: GET XHR localhost:8000/Controller/getUnitSellingPrice [HTTP/1.0 404 Not Found 203ms] In the create.blade View: ...