Display the value on each repeater item

14.3k Views Asked by At

I use jquery repeater to create a dynamic form.

<div class="repeater">
  <table border="1">
    <thead>
      <tr style=>
        <th>ID</th>
        <th>Name</th>
      </tr>
    </thead>
    <tbody data-repeater-list="data">
      <tr data-repeater-item>
        <td><input name="this_id" value="1"></td>
        <td><input name="this_name"></td>
      </tr>
    </tbody>
  </table>
  <button data-repeater-create>Add New</button>
</div>

<script>
  $('.repeater').repeater();

  /* Not working if use below code :
  Reference : https://github.com/DubFriend/jquery.repeater

    $('.repeater').repeater({
      defaultValues: {
        'this_id': '1'
      }
    });
  */
</script>

When I press "add new" button, why does the default value for this_id not appear (blank value). Is there something wrong with my code?

UPDATE : https://jsfiddle.net/b6tryg9m/

2

There are 2 best solutions below

2
On BEST ANSWER

You dint set the type="text" on your input element. Here is a working example:

$('.repeater').repeater({
  defaultValues: {
    'this_id': '1',
    'this_name': 'foo'
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.repeater/1.2.1/jquery.repeater.min.js"></script>
<div class="repeater">
  <table border="1">
    <thead>
      <tr style="background-color:#cecece">
        <th>ID</th>
        <th>Name</th>
      </tr>
    </thead>
    <tbody data-repeater-list="data">
      <tr data-repeater-item>
        <td><input type="text" name="this_id" value="1"/></td>
        <td><input type="text" name="this_name"/></td>
      </tr>
    </tbody>
  </table>
  <br>
  <button data-repeater-create>Add New</button>
</div>

0
On

You have to just definne thw input type

type="text"