- In the HOME EXPENSES section of my Income Tax Organizer, I would like to put a check box to the right of a list field. I found some code at Gravity Forms – How to add checkbox option in list field.
Since I’m not a coder, I don’t know how to customize the following code for my form, located here https://www.nyobc.com/1167-2/?preview_id=1167&preview_nonce=0e400ee441&preview=true:
add_filter( 'gform_column_input_content', 'itsg_gf_checkbox_change_column_content', 99, 6 );
function itsg_gf_checkbox_change_column_content( $input, $input_info, $field, $text, $value, $form_id ) {
// settings
$apply_form_id = 2;
$apply_field_id = 11;
$apply_column_position = 3;
$checkbox_label_and_value = 'Yes';
if ( 'list' == $field->type && $apply_form_id == $form_id && $apply_field_id == $field->id ) {
$has_columns = is_array( $field['choices'] );
if ( $has_columns ) {
foreach( $field['choices'] as $key => $choice ) {
$column_position = $key + 1;
if ( $text == $choice['text'] && $apply_column_position == $column_position ) {
$checked = $value == $checkbox_label_and_value ? "checked" : "";
$input = str_replace( "type='text' ", "type='checkbox' value='" . $checkbox_label_and_value . "' " . $checked . " ",$input );
$input = "<label style='display: inline-block; white-space: nowrap;' >" . $input . " " . $checkbox_label_and_value . "</label>";
}
}
}
}
return $input;
}
add_action( 'gform_enqueue_scripts', 'itsg_gf_checkbox_enqueue_scripts', 10, 2 );
function itsg_gf_checkbox_enqueue_scripts( $form, $is_ajax ) {
// settings
$apply_form_id = 2;
if ( $form['id'] == $apply_form_id ) {
add_action( 'wp_footer', 'itsg_gf_checkbox_script' ); // enqueue inline JavaScript
}
}
function itsg_gf_checkbox_script() {
?>
<script>
jQuery( document ).ready( function($) {
jQuery( '.gfield_list input[type=checkbox]' ).each( function() {
itsg_gf_checkbox_default_value( jQuery( this ) );
});
});
jQuery( '.gfield_list' ).on( 'click', '.add_list_item', function(){
var new_row = jQuery( this ).parents( 'tr.gfield_list_group, tr.gfield_list_row_even, tr.gfield_list_row_odd' ).next( 'tr.gfield_list_group, tr.gfield_list_row_even, tr.gfield_list_row_odd' );
jQuery( new_row ).find( 'input[type=checkbox]' ).each( function() {
itsg_gf_checkbox_default_value( jQuery( this ) );
});
});
function itsg_gf_checkbox_default_value( checkbox ){
if ( checkbox.is(':checked') ) {
checkbox.nextAll( 'input[type=hidden]' ).remove();
} else {
checkbox.after(
jQuery('<input/>', {
'type': 'hidden',
'value': '',
'name': checkbox.attr('name')
})
);
}
}
jQuery( '.gfield_list' ).on( 'click', 'input[type=checkbox]', function(){
itsg_gf_checkbox_default_value( jQuery( this ) );
});
</script>
<?php
}