4

This is my current script

 <script>
        $().ready(function(){

                $('.prettycheckbox').click(function () {
                      $('.prettycheckbox input').removeAttr('checked');
                      $('.prettycheckbox a').removeClass('checked'); 

                      $("a", this).addClass('checked');
                      $("input", this).addAttr("checked");
                });

        });
 </script>

But it's not working well, bceause the part with adding and removing class to links works nice , but for input it doesn't work.

How to make the "checked" checkbox looks like this:

<input class="payment_type" type="checkbox" value="1" name="payment_type" style="display: none;" checked="checked">

and all others look like this?:

<input class="payment_type" type="checkbox" value="1" name="payment_type" style="display: none;">

How to do that? Something like input radio type using jquery and checkboxes?

michaelkonstincs
  • 83
  • 1
  • 1
  • 4
  • 1
    possible duplicate of [jQuery. How to uncheck all checkboxes except one (which was checked)](http://stackoverflow.com/questions/13739115/jquery-how-to-uncheck-all-checkboxes-except-one-which-was-checked) – isherwood Aug 06 '13 at 16:56
  • 1
    The method is `.attr( "checked", "checked" )` but it's not what you want anyway, because the attribute `"checked"` refers to `.defaultChecked` property and you want to change `.checked`. Changing `.defaultChecked` property or `"checked"` attribute will only have effect if the user has not touched the checkbox yet. – Esailija Aug 06 '13 at 16:59
  • For some reason this is working ;D `$('.prettycheckbox input').removeAttr('checked'); $('.prettycheckbox a').removeClass('checked'); $("a", this).addClass('checked'); $("input", this).attr("checked", true);` – michaelkonstincs Aug 06 '13 at 17:06

3 Answers3

12

In,

jQuery 1.6+

To change the property of checkbox you should use the .prop() function.

$('.prettycheckbox input').prop('checked', false);
$('.prettycheckbox input').prop('checked', true);

jQuery 1.5 and below

The .prop() function doesn't exist, but .attr() does similar:

$('.prettycheckbox input').attr('checked', 'checked');

Note: removeAttr is valid, but addAttr is not.

Optimus Prime
  • 6,817
  • 5
  • 32
  • 60
6

Use prop() to change checked value

$('.prettycheckbox input').prop('checked', false);

Something else to remark, method addAttr doesn't exist

Claudio Redi
  • 67,454
  • 15
  • 130
  • 155
  • But doen't this depend on the jquery version, if OP is using older than 1.6, he/she may use .attr() – Optimus Prime Aug 06 '13 at 17:03
  • This is working: `$('.prettycheckbox input').removeAttr('checked'); $('.prettycheckbox a').removeClass('checked'); $("a", this).addClass('checked'); $("input", this).attr("checked", true);` Can you explain why? – michaelkonstincs Aug 06 '13 at 17:04
  • @michaelkonstincs: It's not clear what you're trying to do. It seems that you're trying to check and uncheck on the same method – Claudio Redi Aug 06 '13 at 17:11
  • @ClaudioRedi I thought that the code I provided in my question is clear. I mean click(), then remove, remove, add,add. Hmm, nevermind. it's probably only for advanced jquery users. – michaelkonstincs Aug 06 '13 at 17:22
  • @michaelkonstincs: LOL yeah, your code is a master piece beyond my understanding skills. – Claudio Redi Aug 06 '13 at 17:39
3

try this

document.getElementById("check1").checked = true;
document.getElementById("check1").checked = false;
shijinmon Pallikal
  • 964
  • 11
  • 19