'Add counter jquery

I have problem because I don't know how to change code to add counter in name input. So when I will click #add_input new input will have name name[0], name[1] etc.

  $("#add_input").click(function () {
var html = '';
html += '<div class="row">';
html += '<input type="checkbox" name="name[0]">';
html += '</div></div>';
};


Solution 1:[1]

try this

let i = 0
$("#add_input").click(() => {
  i += 1;
  var html = `<div class="row">
                    <label><input type="checkbox" name="name[]"> input ${i}</label>
                  </div>`;

  $('#result').append(html);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<button id="add_input">add input</button>
<div id="result"></div>

Solution 2:[2]

var counter = 0;

  $("#add_input").click(function () {
    var html = '';
    html += '<div class="row">';
    html += '<input type="checkbox" name="name_' + counter + '"/>';
    html += '</div>';

    counter ++;
};

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sarout
Solution 2