I am trying to create dynamic input fields for a set of predefined labels in a custom widget for the theme I'm working on. I want to achieve something like this:
CourseName FieldONE FieldTWO ------------------------------------------------------------- Chemistry Sprint 2015 Summer 2015 ( - ) Spring 2016 Summer 2016 ( - ) ( + ) ------------------------------------------------------------- Biology Sprint 2015 Summer 2015 ( - ) Fall 2015 Winter 2015 ( - ) Spring 2016 Summer 2016 ( - ) ( + ) -------------------------------------------------------------- Math Fall 2015 Winter 2015 ( - ) ( + ) -------------------------------------------------------------- Physics Fall 2015 Winter 2015 ( - ) ( + ) --------------------------------------------------------------
where CourseName is chemistry, biology, mathematics and physics (only predefined labels, maximum 4) and FieldONE and FieldTWO are dynamic inputs where I want to introduce different terms for each course.
So, if I click on ( + ) , two fields FieldONE and FieldTWO are created for this shortcut. And if I click on ( - ) , both fields will be deleted.
I tried using this Gist , which creates a similar dynamic input as a metabolism, and so far I got this:
<?php <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> <script type="text/javascript"> jQuery(document).ready(function( $ ){ $( '#add-row' ).on('click', function() { var row = $( '.empty-row.screen-reader-text' ).clone(true); row.removeClass( 'empty-row screen-reader-text' ); row.insertBefore( '#repeatable-fieldset-one tbody>tr:last' ); return false; }); $( '.remove-row' ).on('click', function() { $(this).parents('tr').remove(); return false; }); }); </script> <?php foreach ($courses as $course_key => $course_info) { ?> <label><?php echo $course_info['coursecode']; ?></label> <table id="repeatable-fieldset-one" width="100%"> <thead> <tr> <th width="40%">Fall Term</th> <th width="40%">Winter Term</th> <th width="8%"></th> </tr> </thead> <tbody> <?php if ($repeatable_fields) : foreach ($repeatable_fields as $field) { ?> <tr> <td><input type="text" class="widefat" name="name[]" value="<?php if ($field['name'] != '') { echo esc_attr($field['name']); } ?>" /></td> <td> </td> <td><input type="text" class="widefat" name="url[]" value="<?php if ($field['url'] != '') { echo esc_attr($field['url']); } else { echo ''; } ?>" /></td> <td><a class="button remove-row" href="#">Remove</a></td> <td><a id="add-row" class="button" href="#">Add</a></td> </tr> <?php } else : <tr> <td><input type="text" class="widefat" name="name[]" /></td> <td><input type="text" class="widefat" name="url[]" value="" /></td> <td><a class="button remove-row" href="#">Remove</a></td> <td><a id="add-row" class="button" href="#">Add</a></td> </tr> <?php endif; ?> <? } ?> <tr class="empty-row screen-reader-text"> <td><input type="text" class="widefat" name="name[]" /></td> <td><input type="text" class="widefat" name="url[]" value="" /></td> <td><a class="button remove-row" href="#">Remove</a></td> <td><a id="add-row" class="button" href="#">Add</a></td> </tr> </tbody> </table> </p> <?php }
and here is a screenshot:

there are a lot of problems right now, first the "Add" javascript button no longer works, and I'm not sure how to save access data later.
any idea how to make dynamic input fields for labels? It should not be like the code from the existing one that I used, but it would be better if we could make my changes work.
javascript jquery wordpress widget
Rain man
source share