Toggle some action on checkbox with jQuery

Here is how you can toggle an action depending on a checkbox is marked or not. In this case added to a Drupal 6 form item checkbox.

<?php
    $form
['user']['randompwd'] = array(
     
'#type' => 'checkbox',
     
'#title' => t('Set random password: %pwd', array('%pwd' => $randpwd)),
     
'#weight' => ++$w,
     
'#suffix' => '<script type="text/javascript">
                          $(document).ready(function() {                       
                                $("#edit-randompwd").bind("click", function() {
                                    if($("#edit-randompwd").is(":checked")){
                                        $("#edit-pass").val("'
.$randpwd.'");
                                    }else{
                                        $("#edit-pass").val("");
                                    }
                                 });
                        });
                    </script>'
,
    );
?>
Knowledge keywords: