0

I have a list item with a radio button input in each, on click of the list item the input is checked. However, if clicked again I want to remove the class and deselect the option. I can't seem to get anything to work however.

function setupToptions() {
    if ($('ul.top-options input').length) {
        $('ul.top-options li').each(function(){ 
            $(this).removeClass('active');
        });
        $('ul.top-options li input:checked').each(function(){ 
            $(this).parent('li').addClass('active');
        });                
    };
};

http://jsfiddle.net/BKgdc/4/

What is wrong?

3
  • 2
    Well, where's your click handler? Commented Mar 5, 2013 at 16:38
  • As @Jack said; no click handler here. And where do you 'deselect' your radio buttons in the above example? I believe you should use selected and remove attribute selected when working with radio buttons. You're now using checked which only applies on checkboxes Commented Mar 5, 2013 at 16:42
  • @RobinvanBaalen - not true, see this fiddle: jsfiddle.net/ZK4th Commented Mar 5, 2013 at 19:28

5 Answers 5

2
  $('ul.top-options li input[type=radio]').click(function() {
     if(!$(this).closest('li').hasClass('active')){
         $('ul.top-options li').removeClass('active');
         $(this).closest('li').addClass('active');
     }else
         $(this).removeAttr('checked').closest('li').removeClass('active');
});

http://jsfiddle.net/BKgdc/8/

Sign up to request clarification or add additional context in comments.

4 Comments

$('ul.top-options li').removeClass('active'); is effectively the same as $('ul.top-options li').each(function(){ $(this).removeClass('active'); }); but should be faster
also, those should really be removeProp not removeAttr - it is changed in 1.9+ and "might" not work in all cases as documented here: jquery.com/upgrade-guide/1.9/#attr-versus-prop-
I prefer use removeAttr, and removeProp is not recommended for this case, read official documentation note "Note: Do not use this method to remove native properties such as checked, disabled, or selected. This will remove the property completely and, once removed, cannot be added again to element. Use .prop() to set these properties to false instead."
SO, it almost seems as if you should be setting .prop('checked',false) perhaps rather than removing? interesting to me at least :)
1

You need to have an event handler :

$('ul.top-options input').change(function(){
    if ($('ul.top-options input').length) {
        $('ul.top-options li').each(function(){ 
            $(this).removeClass('active');
        });
        $('ul.top-options li input:checked').each(function(){ 
            $(this).parent('li').addClass('active');
        }); 
     }
});

but your code can be simplified as

$('ul.top-options input').change(function(){
     $('ul.top-options li').removeClass('active');
     $('ul.top-options li input:checked').parent('li').addClass('active');
});

See demonstration

Comments

0
$(document).ready(function() {

    $li = $('ul.top-options li');
    $li.click(function() {
        if(!$(this).hasClass('active')){
        $li.removeClass('active')
        .filter(this).addClass('active').find('input').prop('checked', true);
        } else {
            $(this).removeClass('active');
            $(this).find('input').prop('checked', false);
        }
    });
    $li.filter(':has(:checked)').click();

});

Comments

0

Your solution would be to use checkboxes instead of radio buttons. No need for any javascript when using checkboxes.

If you want to deselect a radio button you should remove it's selected attribute. :checked only applies on checkboxes.

Comments

0

You would likely need to do this:

$li = $('ul.top-options li');
$li.click(function () {
    var self = $(this);
    $li.not(self).removeClass('active');
    (self.hasClass('active') ? (self.removeClass('active').find('input').prop('checked', false)) : (self.addClass('active').find('input').prop('checked', true)));
});
$li.filter(':has(:checked)').click();

alternate conditional form:

$li = $('ul.top-options li');
$li.click(function () {
    var self = $(this);
    $li.not(self).removeClass('active');
    if (self.hasClass('active')) {
        self.removeClass('active').find('input').prop('checked', false);
    } else {
        self.addClass('active').find('input').prop('checked', true);
    }
});
$li.filter(':has(:checked)').click();

a fiddle to show in action: http://jsfiddle.net/ZK4th/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.