Search code examples
jqueryhtmljquery.repeater

Display the value on each repeater item


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/


Solution

  • 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>