5

I am using the prettyCheckable jquery plugin to make my checkboxes and radio buttons look pretty and if I use jquery to set the property checked it does not work because the plugin hides the actual checkbox and makes it's own override HTML to pretty up the checkbox/radio, therefore the jquery doesn't recognize when I check the radio or checkbox

//on button click I tried all the things, but nothing is working. please help

<input type="radio" id="rdTypeAdd" name="taxationtype" class="radiobutton" value="+" data-     label="Additive" data-customclass="margin-right" checked="checked" />    
<input type="radio" id="rdTypeSub" name="taxationtype" class="radiobutton" value="-" data-label="Subtractive" data-customclass="margin-right" />


    $('#rdTypeAdd').buttonset();
    $('[name="rdTypeAdd"][value="+"]').prop("checked", true);
    $('#rdTypeAdd').buttonset("refresh");

    //$('[name="rdTypeAdd"][value="+"]').attr("checked", true);
    //$('[name="rdTypeAdd"][value="+"]').prop("checked", true).trigger("change");

    //($('.rdTypeAdd').prop('checked',true));
Muhammad Dyas Yaskur
  • 6,914
  • 10
  • 48
  • 73
user2674021
  • 51
  • 1
  • 3

5 Answers5

3

Not sure how much of a hack this is but I was able to get a div to check and uncheck a prettyCheckable checkbox. I noticed when I removing the class="checked" from the that prettyCheckable includes the checkbox became unchecked.

$("#checkbox").prettyCheckable();
    $("#checkboxDiv").click(function() {
    if ($("#checkbox").is(':checked'))
    {
        $("#checkbox").next().removeClass("checked");
        $('#checkbox').each(function(){ this.checked = false; });
    } else {
        $("#checkbox").next().addClass("checked");
        $('#checkbox').each(function(){ this.checked = true; });
    }
});

Adding or removing the "checked" class from the (which is .next() to the prettyCheckable element) and setting checked=true in the original checkbox element are both needed for it to work. Yet my if cookie is set piece is able to check the box with only the 's class. Like I said I don't know how bad of a hack this is but it worked for me hopefully it helps you out. Not sure why prettyCheckable doesn't have this ability or atleast document it because I could not find anything.

Micah
  • 31
  • 2
  • I ended up running into more issues with prettyCheckable and found several other jQuery checkbox plugins that worked much better on unheap.com – Micah Nov 08 '13 at 18:01
3

You can use pretty checkable functions:

$('[name="rdTypeAdd"][value="+"]').prettyCheckable('check');

and

$('[name="rdTypeAdd"][value="+"]').prettyCheckable('uncheck');

kravits88
  • 12,431
  • 1
  • 51
  • 53
1

This answer builds off of @kravits88 and this github issue

var $radio = $('[name="rdTypeAdd"][value="+"]');
$radio.prettyCheckable('check');
$radio.prop('checked', true).change();
$radio.click();
pymarco
  • 7,807
  • 4
  • 29
  • 40
0

This seems to work for me. Essentially you have to find the tag parent that is created by prettyCheckable around your checkbox and set that as well as the input:

$("#element").prop("checked", true).parent().find("a:first").addClass("checked");
Lucas Young
  • 187
  • 2
  • 13
-1

Setting the "checked" attribute to anything should work as shown here.

$('#rdTypeAdd').attr("checked","checked");
Community
  • 1
  • 1
Rick Sullivan
  • 178
  • 1
  • 7
  • prettyCheckable jquery plugin hides the actual checkbox and makes it's own override html which is as follows
    if it is simple radio button then your code i.e $('#rdTypeAdd').attr("checked","checked"); might work
    – user2674021 Aug 29 '13 at 11:32
  • even code do not work as radio button is wrrapped in prettycheckable html //$('[name="rdTypeAdd"][value="+"]').prop("checked", true); // $('#rdTypeAdd').buttonset("refresh"); //$('[name="rdTypeAdd"][value="+"]').addClass("checked"); //$('[name="rdTypeAdd"][value="+"]').prop("checked", true).trigger("change"); //$('#rdTypeAdd').buttonset("refresh"); //var value = $('[name="rdTypeAdd"]:checked').val(); – user2674021 Aug 29 '13 at 11:39