1

So i have this code

<ul id="id_priori">
    <li>
        <label for="id_priori_0">
            <input id="id_priori_0" name="priori" type="radio">Hola
        </label>
    </li>
</ul>

And this code

$("#id_tipo").on('click', 'li', function () {
    var self = $(this),
        radio = self.find(":radio")[0];

    radio.checked = !radio.checked;

    self.toggleClass('on', radio.checked);

})
    .find(":radio").each(function () {
    $(this).closest('li').toggleClass('on', this.checked);
});

What i want is to unclass all the labels whose radio's arent checked , any idea ?

charlietfl
  • 170,828
  • 13
  • 121
  • 150

3 Answers3

1
$(":radio:not(checked)").each(function () {
    $(this).closest('li').removeClass('on');
});

Anyway, assuming all radios are connected, and there is always only one checked, the whole code could be simpler:

$("#id_priori").on('click', 'li', function () {
    $(this).find(':radio').prop('checked', true);
    $(this).addClass('on').siblings().removeClass('on');
});

Take a look at this Fiddle. We can work from this point

EduSanCon
  • 1,749
  • 1
  • 11
  • 9
0

Assuming you mean remove all class from each <label> add it in the find:

 .find(":radio").each(function () {
    if( !this.checked){
       $(this).parent('label').removeAttr('class');
    }
    $(this).closest('li').toggleClass('on', this.checked);
});
charlietfl
  • 170,828
  • 13
  • 121
  • 150
0

if you want to remove class just go with @littleLouito Code else if you want toggle try this one

   $(":radio:not(checked)").each(function () {
      $(this).closest('label').toggleClass("on", this.checked);
    });
user3419304
  • 249
  • 2
  • 4
  • 20
  • Cool but for some reason when i click the label it doesnt does anything , its like if something is canceling it –  Jul 27 '14 at 20:10