I have radio buttons like this:
<input class="check" type="radio" id="radio1" name="group"/>
<label for="radio1">vrai</label>
<input class="check" type="radio" id="radio2" name="group"/>
<label for="radio2">faux</label>
<input class="check" type="radio" id="radio3" name="group"/>
<label for="radio3">75</label>
<input class="check" type="radio" id="radio4" name="group"/>
<label for="radio4">18</label>
I had the selected class when that are :checked
, but strangely even using ie7.js it does not work in ie8.
Here is my js:
$(document).ready(function() {
$('input:checked').addClass("selected");
$('input').click(function () {
$('input:not(:checked)').removeClass("selected");
$(this).addClass("selected");
});
});
not sure if it has to do but I would rewrite the following and see if it changes something:
$('input').not('input:checked').removeClass("selected");
$(this).addClass("selected");
Try using change()
instead of click()
$(document).ready(function() {
$('input:checked').addClass("selected");
$('input').change(function () { // <--- HERE
$('input:not(:checked)').removeClass("selected");
$(this).addClass("selected");
});
});
Hy, i have made pure javascript solution for you which works accross all browser. please have a look if it is useful for you
<script type="text/javascript">
function check(selected)
{
var inputs= document.getElementsByTagName('input');
for(i=0 ; i < inputs.length; ++i)
{
inputs[i].className="";
}
selected.className="selected";
}
</script>
<input class="check" type="radio" id="radio1" name="group" onclick="check(this);"/>
<label for="radio1">vrai</label>
<input class="check" type="radio" id="radio2" name="group" onclick="check(this);"/>
<label for="radio2">faux</label>
<input class="check" type="radio" id="radio3" name="group" onclick="check(this);"/>
<label for="radio3">75</label>
<input class="check" type="radio" id="radio4" name="group" onclick="check(this);"/>
<label for="radio4">18</label>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.