0

I want to display radio buttons as tabs. Following this codepen, I've attempted this in my project. But when I click on pseudo tab, nothing happens. It doesn't get checked.

How can I achieve the effect?

input.tgl-radio-tab-child {
  position: absolute;
  left: -99999em;
  top: -99999em;
  opacity: 1;
  z-index: 1;
}

input.tgl-radio-tab-child+label {
  cursor: pointer;
  float: left;
  border: 1px solid #aaa;
  margin-right: -1px;
  padding: .5em 1em;
  position: relative;
}

input.tgl-radio-tab-child+label:hover {
  background-color: #eee;
}

[type=radio]:checked+label {
  background-color: #c30;
  z-index: 1;
}
<label for="abc">ABC<span style="color: red;">*</span></label>
<div class="form-check">
  <div class="tgl-radio-tabs">
    <input type="radio" class="form-check-input tgl-radio-tab-child" name="abcorigin"><label class="radio-inline">ABCDEFGHIJKL</label>
    <input type="radio" class="form-check-input tgl-radio-tab-child" name="abcorigin"><label class="radio-inline">MNOPQRSTUVWXYZ</label>
  </div>
</div>

1
  • 1
    You have to add the for attribute to each label to link it to an input, which must be set equal to the id attribute of the input element. Review the codepen more closely. Commented Jul 18, 2017 at 19:38

1 Answer 1

5

You need to bind your labels to your inputs via the for attribute which should reference the respective input's ID:

input.tgl-radio-tab-child {
  position: absolute;
  left: -99999em;
  top: -99999em;
  opacity: 1;
  z-index: 1;
}

input.tgl-radio-tab-child+label {
  cursor: pointer;
  float: left;
  border: 1px solid #aaa;
  margin-right: -1px;
  padding: .5em 1em;
  position: relative;
}

input.tgl-radio-tab-child+label:hover {
  background-color: #eee;
}

[type=radio]:checked+label {
  background-color: #c30;
  z-index: 1;
}
<label for="abc">ABC<span style="color: red;">*</span></label>
<div class="form-check">
  <div class="tgl-radio-tabs">
    <input id="x" type="radio" class="form-check-input tgl-radio-tab-child" name="abcorigin"><label for="x" class="radio-inline">ABCDEFGHIJKL</label>
    <input id="y" type="radio" class="form-check-input tgl-radio-tab-child" name="abcorigin"><label for="y" class="radio-inline">MNOPQRSTUVWXYZ</label>
  </div>
</div>

1
  • 1
    That was it! Thank you.
    – input
    Commented Jul 18, 2017 at 19:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.