Monday, August 13, 2012

Check and Uncheck Checkbox using JQuery


<input type="checkbox" name="checkboxname" value="checkboxvalue" />
<input type="button" onclick="show_checked()" value="Show" />
<input type="button" onclick="Change_checked(true)" value="On" />
<input type="button" onclick="Change_checked(false)" value="Off" />

<script type="text/javascript">
    function show_checked() {
        alert($('input[name=foo]').is(':checked'));
        //alert($('input[name=foo]').attr('checked'));
    }
    function Change_checked(checkvalue) {
        if (checkvalue) {
           //use \" before and after checkboxname for avoid slash(/) inside the name
            $('input[name =\"checkboxname\"]').attr("checked", checkvalue)
        }
        else {
            $('input[name = \"checkboxname\" ]').attr("checked", false)
            $('input[name = \"checkboxname\" ]').removeAttr("checked")
        }
    }
    
</script>

No comments:

Post a Comment