Home > Net >  Add counter jquery
Add counter jquery

Time:01-28

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 >';
html  = '<input type="checkbox" name="name[0]">';
html  = '</div></div>';
};

CodePudding user response:

try this

let i = 0
$("#add_input").click(() => {
  i  = 1;
  var html = `<div >
                    <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>

CodePudding user response:

var counter = 0;

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

    counter   ;
};
  •  Tags:  
  • Related