Caroline K
Caroline K

Reputation: 25

Radio Button Not Working on Click

This is my first question so please forgive me if I am unclear, ask an ignorant question or break any written unwritten programming rules. I am a very beginner coder and promise that is not my intention!

I am working on a simple donation form for my company. There will be a set of buttons where a donor can choose from designated predetermined amounts or manually enter their own amount. I am using radio buttons that I have designed to look like block buttons. I am having trouble getting the button to stay checked (the selected button should stay red). I have tried everything I can think for troubleshooting and feel like I'm right back where I started. Any help is so greatly appreciated!

.amount_list input {
  z-index: 999;
  visibility: hidden;
  position: absolute;
}
label {
  padding: 10px 20px;
  border: 1px solid #d50032;
  cursor: pointer;
  z-index: 90;
  border-radius: 5px;
  background: #fff;
  font-family: Arial, sans-serif;
  font-size: 14px;
  font-weight: bold;
  color: #d50032;
  display: inline-block;
  float: left;
  margin: 5px 10px 5px 0;
}
.amount_list {
  margin-left: auto;
  margin-right: auto;
  overflow: auto;
  float: left;
}
.gift_amount:hover {
  background: #d50032;
  color: #fff;
  border-radius: 5px;
  text-align: center;
  font-family: Arial, sans-serif;
}
.gift_amount + input:checked {
  -webkit-filter: none;
  -moz-filter: none;
  filter: none;
  background: #d50032;
  border: 1px solid #d50032;
  opacity: ;
  border-radius: 5px;
  text-align: center;
  font-family: Arial, sans-serif;
  color: #fff;
}
#inputother {
  border-radius: 5px;
  border: 1px solid #ccc;
  visibility: visible;
  padding: 12px 5px;
  margin: 0 5px;
}
<div class="amount_list">
  <label class="gift_amount">
    <input type="radio" value="1000" name="amount" />$1,000</label>
  <label class="gift_amount">
    <input type="radio" value="500" name="amount" />$500</label>
  <label class="gift_amount">
    <input type="radio" value="250" name="amount" />$250</label>
  <label class="gift_amount">
    <input type="radio" value="100" name="amount" />$100</label>
  <label class="gift_amount" for="other">
    <input type="radio" value="other" name="amount" id="other">Other</label>
  <input id="inputother" type="text" onchange="changeradioother()" />
</div>

As I mentioned, I am a novice when it comes to coding so any mistakes, sloppy code or bad habits it looks I can learn from, please let me know. Thank you!

Upvotes: 2

Views: 6788

Answers (2)

Jacob G
Jacob G

Reputation: 14172

You are trying to style an elements parent, which can't be done with CSS.

Wrap the price in a <span>, and style that instead:

.amount_list input {
  z-index: 999;
  visibility: hidden;
  position: absolute;
}
label span {
  padding: 10px 20px;
  border: 1px solid #d50032;
  cursor: pointer;
  z-index: 90;
  border-radius: 5px;
  background: #fff;
  font-family: Arial, sans-serif;
  font-size: 14px;
  font-weight: bold;
  color: #d50032;
  display: inline-block;
  float: left;
  margin: 5px 10px 5px 0;
}
.amount_list {
  margin-left: auto;
  margin-right: auto;
  overflow: auto;
  float: left;
}
.gift_amount:hover span {
  background: #d50032;
  color: #fff;
}
.gift_amount input:checked + span {
  background: #d50032;
  color: #fff;
}
<div class="amount_list">
  <label class="gift_amount">
    <input type="radio" value="1000" name="amount" />
    <span>$1,000</span>
  </label>
  <label class="gift_amount">
    <input type="radio" value="500" name="amount" />
    <span>$500</span>
  </label>
  <label class="gift_amount">
    <input type="radio" value="250" name="amount" />
    <span>$250</span>
  </label>
  <label class="gift_amount">
    <input type="radio" value="100" name="amount" />
    <span>$100</span>
  </label>
  <label class="gift_amount" for="other">
    <input type="radio" value="other" name="amount" id="other">
    <span>Other</span>
  </label>
</div>

Upvotes: 2

Asons
Asons

Reputation: 87303

For that to work you have to make a markup change + correction of the CSS rule, where you put the input before the label and swap the CSS to

input:checked + .gift_amount {...}

An id is also needed along with the for attribute, to pair the label with its input

Sample

.amount_list input {
  z-index: 999;
  visibility: hidden;
  position: absolute;
}
label {
  padding: 10px 20px;
  border: 1px solid #d50032;
  cursor: pointer;
  z-index: 90;
  border-radius: 5px;
  background: #fff;
  font-family: Arial, sans-serif;
  font-size: 14px;
  font-weight: bold;
  color: #d50032;
  display: inline-block;
  float: left;
  margin: 5px 10px 5px 0;
}
.amount_list {
  margin-left: auto;
  margin-right: auto;
  overflow: auto;
  float: left;
}
.gift_amount:hover {
  background: #d50032;
  color: #fff;
  border-radius: 5px;
  text-align: center;
  font-family: Arial, sans-serif;
}
input:checked + .gift_amount {
  background: #d50032;
  border: 1px solid #d50032;
  opacity: ;
  border-radius: 5px;
  text-align: center;
  font-family: Arial, sans-serif;
  color: #fff;
}
#inputother {
  border-radius: 5px;
  border: 1px solid #ccc;
  visibility: visible;
  padding: 12px 5px;
  margin: 0 5px;
}
<div class="amount_list">
  
  <input id="a1000" type="radio" value="1000" name="amount" />
  <label for="a1000" class="gift_amount">$1,000</label>

  <input id="a500" type="radio" value="500" name="amount" />
  <label for="a500" class="gift_amount">$500</label>

</div>

Upvotes: 0

Related Questions