0

I have a list of checkbox and want to select the only one check box and add the class for the checkbox input and select another checkbox remove that class from the existing checkbox

 $('input.myclass').click(function () 
  {
        var id =  $(this).attr('id').replace('image-','');
        $('input.myclass:checked').not(this).removeAttr('checked');

        var sFilter = "";
        $('input.myclass[type=checkbox]').each(function () {
            sFilter = sFilter + (this.checked ? $(this).val() : "");
        });
    check();    
    });

    function check(){
    var a = $('input:checkbox[name=cover_image]:checked').val();
    alert(a);
    }

I want to select only one checkbox and class for the checked checkbox if that checkbox not checked then remove the class for that

4
  • 4
    Since you only want to select one, wouldn't radio buttons be more appropriate? Commented Nov 18, 2013 at 4:20
  • I want to select only one checkbox and class for the checked checkbox if that checkbox not checked then remove the class for that Commented Nov 18, 2013 at 4:24
  • Users expect certain behaviors for different types of input. You should use radio buttons if you want to only allow one choice. Over-riding the expected and default behavior of checkboxes will only confuse and frustrate your user. Commented Nov 18, 2013 at 5:09
  • Just use CSS, no script required. Commented Nov 18, 2013 at 5:18

3 Answers 3

1

If you really want to use checkboxes(and not radio buttons) for some reason, do something like this:

$('input:checkbox').on('click', function () {
    $('input.selected').removeClass('selected').prop('checked', false);
    $('input:checked').addClass('selected');
});

Edit: Removing the attribute works, but property manipulation is a slighty better way of doing it(as suggested by RobG)

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

2 Comments

Adding and removing the checked attribute does not check or uncheck a checkbox, it sets whether or not the checkbox is checked by default (at least it does in versions of jQuery that distinguish between attributes and properties).
@RobG I'm using jQuery 1.10.2 with Chrome V32, and it's unchecking the box when I do removeAttr('checked'). It's even checking the box with attr('checked', true). The only odd thing is that it doesn't check the box with that code once you uncheck it using removeAttr
1

For your purpose first remove classes from all checkbox and unchecked them and then add class to clicked checkbox and checked it as below

$("input[type='checkbox']").on('click', function () {
    $("input[type='checkbox']").removeClass('selected').attr('checked', false);
    $(this).addClass('selected').attr('checked', true);
});

Check this Fiddle for your question

Comments

1

you may use on() to listen the changes on group of checkboxes.

var $checkBoxes = $(":checkbox").on("change", function () { // Here listening the changes on checkbox using on()
  $checkBoxes.removeClass("change").attr("checked", false); // remove the class from existing Checkbox
  $(this).addClass("change").attr("checked", true); // adding the class for the currenly checked checkbox
});

working FIDDLE is here

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.