0

I want to generate Id's for input type text whenever I click on .add class. Currently Whenever I click on .add class it generates the id's with same name for each input control. So while adding data it does not work properly.

So how should I generate ID's for each text whenever I click on .add class. Please suggest.

Here is what is generates.

Generated HTML

$('.add').on('click', function() {
  var $tr = $(this).closest('tr');
  var $tr2 = $tr.clone(true, true);
  $tr2.find(".vendorName").children('label').remove();
  $tr2.find(".vendorFromDate").children('label').remove();
  $tr2.find(".vendorToDate").children('label').remove();
  $tr2.find(".vendorName").children().unwrap();
  $tr2.find(".vendorFromDate").children().unwrap();
  $tr2.find(".vendorToDate").children().unwrap();
  $tr2.insertAfter($tr);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<tr>
  <td>
    <div class="row noPadding vendorForm">
      <div class="vendorDaterow">
        <div class="vendorName" id="dvVendorNameData">
          <label>SP Vender Name</label><span><input type="text" name="nmVendorData" id="txtVendorName1" /></span>
        </div>
        <div class="vendorFromDate">
          <label>From Date</label><span class="datepicker"><input type="text" name="nmVendorData" id="spFromDate1" /><i class="fa fa-calendar" aria-hidden="true"></i></span>
        </div>
        <div class="vendorToDate">
          <label>To Date</label><span class="datepicker"><input type="text" name="nmVendorData" id="spToDate1" /><i class="fa fa-calendar" aria-hidden="true"></i></span>
        </div>
      </div>
      <div class="add">
        <i class="fa fa-plus" aria-hidden="true"></i>
      </div>
      <i class="max">(Maximum 5 Vendors)</i>

    </div>
  </td>
</tr>

Please suggest how should I generate the dynamic ID.

Nad
  • 4,605
  • 11
  • 71
  • 160
  • @Satpal: sorry, it may be. but I want the idea for generating the id's which is not asked by me – Nad Dec 07 '17 at 12:03
  • @Satpal: I was expecting some more suggestion from him but he didnt replied.So posted this with starting that I want to generate ID's – Nad Dec 07 '17 at 12:05
  • Hope this helps https://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript – Ashish Kumar Dec 07 '17 at 12:07
  • @AshishKumar: i guess that's something not same – Nad Dec 07 '17 at 12:12

2 Answers2

1

Check this

(function () {
  var toAddCloneCount = 2;

$('.add').on('click', function() {
  var $tr = $(this).closest('tr');
  var $tr2 = $tr.clone(true, true);
  $tr2.find(".vendorName").children('label').remove();
  $tr2.find(".add").children().remove();
  $tr2.find(".vendorFromDate").children('label').remove();
  $tr2.find(".vendorToDate").children('label').remove();
  $tr2.find('#txtVendorName').prop('id', 'txtVendorName' + toAddCloneCount);
  $tr2.find('#spFromDate1').prop('id', 'spFromDate' + toAddCloneCount);
  $tr2.find('#spToDate1').prop('id', 'spToDate' + toAddCloneCount++);
  $tr2.insertAfter($tr);
});
})();
.vendorName,.vendorFromDate,.vendorToDate{
width:33%;float:left;}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
                            <td>
                                <div class="row noPadding vendorForm">
                                    <div class="vendorDaterow">
                                        <div class="vendorName">
                                            <label>SP Vender Name</label><span>@*Shri Kamalkanth Co.*@<input type="text" name="nmVendorData" id="txtVendorName" /></span> 
                                        </div>
                                        <div class="vendorFromDate">
                                            <label>From Date</label><span class="datepicker"><input type="text" id="spFromDate1" /><i class="fa fa-calendar" aria-hidden="true"></i></span>
                                        </div>
                                        <div class="vendorToDate">
                                            <label>To Date</label><span class="datepicker"><input type="text" id="spToDate1" /><i class="fa fa-calendar" aria-hidden="true"></i></span>
                                        </div>
                                    </div>
                                    <div class="add">
                                        <i class="fa fa-plus" aria-hidden="true"></i>
                                    </div>
                                    <i class="max">(Maximum 5 Vendors)</i>

                                </div>
                            </td>
                        </tr>
</table>
Hiren Vaghasiya
  • 5,454
  • 1
  • 11
  • 25
0

Well, you can always generate IDs based on how many items you have.

you will have something like

            $(".add").on("click", function(){
            var numBlocks = $('.vendorDaterow').length; 
/*gets number of elements with this class in DOM*/

            var formStructure = "";
            formStructure = "<div id='formRow"+numBlocks+"' class='vendorDaterow'>";
            formStructure = "<div class='vendorName' id='dvVendorNameData"+numBlocks+"'>";
            formStructure = "<label>SP Vender Name</label><span><input type='text' name='nmVendorData' id='txtVendorName"+numBlocks+"' />";
            /*continue for whole structure*/

            $("#formRow"+(numBlocks-1)).parent().append(formStructure);
     /*finds last existing #formRow, gets his parent node and append your new node into it. Also you can do your other logic based on id, like hiding the add button or something.*/


        });

note that counting IDs starts at 0 (you have nothing, lenght is 0. so the first added node will will be #formRow0.

Zorak
  • 709
  • 7
  • 24