dllhell dllhell - 3 months ago 16
jQuery Question

Set radio buttons by value using jquery on non-checkboxradio object (buttons behave differently with css)

As you can see in snippet, my radio buttons behave differently with css. I am troubled to understand what is the problem and how to handle this properly.
I can't call

refresh
, because they are not initialised as jquery
checkboxradio
.

Without CSS, radio buttons behaves as expected but with CSS, well, may be expected for you with more knowledge but unwanted in my case.



$("#btn0").click(function(e) {

document.getElementById("rbtShareExp0").checked = true;

});


$("#btn1").click(function(e) {

document.getElementById("rbtShareExp1").checked = true;

});

$("#btn0a").click(function(e) {

document.getElementById("rbtShareExp0_a").checked = true;

});


$("#btn1a").click(function(e) {

document.getElementById("rbtShareExp1_a").checked = true;

});

.switch-field {
font-family: Arial;
overflow: hidden;
}
.switch-title {
margin-bottom: 6px;
}
.switch-field input {
display: none;
}
.switch-field label {
float: left;
}
.switch-field label {
display: inline-block;
background-color: #D9EDF7;
color: #1159a5;
font-size: 12px;
font-weight: bold;
text-align: center;
padding: 6px 14px;
border: 1px solid rgba(0, 0, 0, 0.2);
-webkit-box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px rgba(255, 255, 255, 0.1);
box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px rgba(255, 255, 255, 0.1);
-webkit-transition: all 0.3s ease-in-out;
-moz-transition: all 0.3s ease-in-out;
-ms-transition: all 0.3s ease-in-out;
-o-transition: all 0.3s ease-in-out;
transition: all 0.3s ease-in-out;
}
.switch-field label:hover {
cursor: pointer;
}
.switch-field input:checked + label {
background-color: #A5DC86;
-webkit-box-shadow: none;
box-shadow: none;
}
.switch-field label:first-of-type {
border-radius: 4px 0 0 4px;
}
.switch-field label:last-of-type {
border-radius: 0 4px 4px 0;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<fieldset>
<legend>with CSS:</legend>
<div class="switch-field">
<label for="rbtShareExp0">No way</label>
<input type="radio" class="rbtShare" name="rbtShareExp" id="rbtShareExp0" value="0" />
<label for="rbtShareExp1">Yes, share it</label>
<input type="radio" class="rbtShare" name="rbtShareExp" id="rbtShareExp1" value="1" />
</div>

<input type='button' id='btn0' value='set to 0' />
<input type='button' id='btn1' value='set to 1' />
</fieldset>


<fieldset>
<legend>without CSS:</legend>

<label for="rbtShareExp0">No way</label>
<input type="radio" class="rbtShare" name="rbtShareExp" id="rbtShareExp0_a" value="0" />
<label for="rbtShareExp1">Yes, share it</label>
<input type="radio" class="rbtShare" name="rbtShareExp" id="rbtShareExp1_a" value="1" />
</br>
<input type='button' id='btn0a' value='set to 0' />
<input type='button' id='btn1a' value='set to 1' />
</fieldset>




Answer

This is due to your styling rules. Your active state for the styled labels depends on .switch-field input:checked + label meaning that if you check the first radio, it will activate the second label. Thus, you should just make the labels go after the radio buttons, like I have done below and they will work correctly:

$("#btn0").click(function(e) {

  document.getElementById("rbtShareExp0").checked = true;

});


$("#btn1").click(function(e) {

  document.getElementById("rbtShareExp1").checked = true;

});

$("#btn0a").click(function(e) {

  document.getElementById("rbtShareExp0_a").checked = true;

});


$("#btn1a").click(function(e) {

  document.getElementById("rbtShareExp1_a").checked = true;

});
.switch-field {
  font-family: Arial;
  overflow: hidden;
}
.switch-title {
  margin-bottom: 6px;
}
.switch-field input {
  display: none;
}
.switch-field label {
  float: left;
}
.switch-field label {
  display: inline-block;
  background-color: #D9EDF7;
  color: #1159a5;
  font-size: 12px;
  font-weight: bold;
  text-align: center;
  padding: 6px 14px;
  border: 1px solid rgba(0, 0, 0, 0.2);
  -webkit-box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px rgba(255, 255, 255, 0.1);
  box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px rgba(255, 255, 255, 0.1);
  -webkit-transition: all 0.3s ease-in-out;
  -moz-transition: all 0.3s ease-in-out;
  -ms-transition: all 0.3s ease-in-out;
  -o-transition: all 0.3s ease-in-out;
  transition: all 0.3s ease-in-out;
}
.switch-field label:hover {
  cursor: pointer;
}
.switch-field input:checked + label {
  background-color: #A5DC86;
  -webkit-box-shadow: none;
  box-shadow: none;
}
.switch-field label:first-of-type {
  border-radius: 4px 0 0 4px;
}
.switch-field label:last-of-type {
  border-radius: 0 4px 4px 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<fieldset>
  <legend>with CSS:</legend>
  <div class="switch-field">
    <input type="radio" class="rbtShare" name="rbtShareExp" id="rbtShareExp0" value="0" /><label for="rbtShareExp0">No way</label>
    
    <input type="radio" class="rbtShare" name="rbtShareExp" id="rbtShareExp1" value="1" /><label for="rbtShareExp1">Yes, share it</label>
    
  </div>

  <input type='button' id='btn0' value='set to 0' />
  <input type='button' id='btn1' value='set to 1' />
</fieldset>


<fieldset>
  <legend>without CSS:</legend>

  <label for="rbtShareExp0">No way</label>
  <input type="radio" class="rbtShare" name="rbtShareExp" id="rbtShareExp0_a" value="0" />
  <label for="rbtShareExp1">Yes, share it</label>
  <input type="radio" class="rbtShare" name="rbtShareExp" id="rbtShareExp1_a" value="1" />
  </br>
  <input type='button' id='btn0a' value='set to 0' />
  <input type='button' id='btn1a' value='set to 1' />
</fieldset>