Joe
Joe

Reputation: 586

How to append textbox value to dynamically add fields?

I have one input field and one bootstrap modal box, i want to apend value to dynamically added rows continuesly,

for example if i click add row and enter some values like "Joe" whenever i click add row that value (Joe) apend every rows.

I HOPE MY QUESTION IS UNDERSTANDABLE.

Example Fiddle here..

FIDDLE HERE..

Example code here..

$(document).ready(function() {

  $("#add_Row").on("click", function() {
    var counter = 0;
    var idVal = $('#tab_logic tr:last').find('td:first').html();
    var matches = idVal.match(/\d+/g);
    if (matches != null) {
      counter = Number(matches) + counter + 1;
    }
    var newRow = $("<tr>");
    var cols = "";

    cols += '<td><input type="text" class="form-control required price" name="narr" placeholder="Enter your text here" id="acc_narrat' + counter + '" data-toggle="modal" data-target="#narratModal" onchange="unname(this.id, this.value)"/></td>';

    cols += '<td><button type="button" class="adRow ibtnDel" style="width:70%;">x</button></a></td>';

    newRow.append(cols);


    var defVal = $("select[name=acctname]").find(":selected").val();
    if (defVal) {
      $("select[name=accountName]").find(`option[value=${defVal}]`).hide();
    }
    $("table.order-list").append(newRow);
    setValCashVal('accountName'.concat(counter));
    bindScript();
    counter++;

  });
  $("table.order-list").on("click", ".ibtnDel", function(_event) {
    $(this).closest("tr").remove();
    evaluateTotal();
  });
});

/* Apend value to all row */
$("#ok_button").on('click', function() {
  let val = $("#cash_text").val();
  $("#pay_narrat, #acc_narrat").val(val);
});
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>


<input type="button" class="add_Row adRow" id="add_Row" value="Add Row">
<table class="table table-bordered table-hover order-list" id="tab_logic" style="width:60% !important">
  <thead>
    <tr style="background-color: #680779; color: #fff;">
      <th class="text-center">
        Narration*
      </th>
    </tr>
  </thead>
  <tbody>
    <tr id="fst_row">
      <td>

      </td>
    </tr>
  </tbody>
</table>

<!-- narrtion modal -->

<div class="modal fade" id="narratModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close cash-dismiss" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
        <h4 class="modal-title modal_head" id="myModalLabel">Narration</h4>
      </div>
      <div class="modal-body">
        <label class="modal_note">Note: Only alphabets A-Z a-z number 0-9 and characters % [] () - _ . , are allowed in text</label>
        <textarea id="cash_text" class="cash_text" maxlength="200">Enter Here...</textarea>
        <span class="modal_valid">0/200 Characterts entered</span>
      </div>
      <div class="modal-footer narr_footer">
        <button id="ok_button" type="button" class="btn btn-primary cashmodal_btn" data-dismiss="modal">OK</button>
      </div>
    </div>
  </div>
</div>

Upvotes: 0

Views: 84

Answers (1)

jishan siddique
jishan siddique

Reputation: 1895

Please check this one working for me.

For check working demo click Here

Html Code

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>


<input type="button" class="add_Row adRow" id="add_Row" value="Add Row">
<table class="table table-bordered table-hover order-list" id="tab_logic" style="width:60% !important">
  <thead>
    <tr style="background-color: #680779; color: #fff;">
      <th class="text-center">
        Narration*
      </th>
    </tr>
  </thead>
  <tbody>
    <tr id="fst_row">
      <td>

      </td>
    </tr>
  </tbody>
</table>

<!-- narrtion modal -->

<div class="modal fade" id="narratModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close cash-dismiss" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
        <h4 class="modal-title modal_head" id="myModalLabel">Narration</h4>
      </div>
      <div class="modal-body">
        <label class="modal_note">Note: Only alphabets A-Z a-z number 0-9 and characters % [] () - _ . , are allowed in text</label>
        <textarea id="cash_text" class="cash_text" maxlength="200">Enter Here...</textarea>
        <span class="modal_valid">0/200 Characterts entered</span>
      </div>
      <div class="modal-footer narr_footer">
        <button id="ok_button" type="button" class="btn btn-primary cashmodal_btn" data-dismiss="modal">OK</button>
      </div>
    </div>
  </div>
</div>

Jquery Code

        $(document).ready(function() {
                    $("#ok_button").on("click",function(){
          $("#acc_narrat0").val($("#cash_text:first").val());
          });
          $("#add_Row").on("click", function() {
            var counter = 0;
            var idVal = $('#tab_logic tr:last').find('td:first').html();
            var matches = idVal.match(/\d+/g);
            if (matches != null) {
              counter = Number(matches) + counter + 1;
            }
            var newRow = $("<tr>");
            var cols = "";

           cols += '<td><input value="'+$("#cash_text:first").val()+'" type="text" class="form-control required price" name="narr" placeholder="Enter your text here" id="acc_narrat' + counter + '" data-toggle="modal" data-target="#narratModal" onchange="unname(this.id, this.value)"/></td>'

            cols += '<td><button type="button" class="adRow ibtnDel" style="width:70%;">x</button></a></td>';

            newRow.append(cols);


            var defVal = $("select[name=acctname]").find(":selected").val();
            if (defVal) {
              $("select[name=accountName]").find(`option[value=${defVal}]`).hide();
            }
            $("table.order-list").append(newRow);
            setValCashVal('accountName'.concat(counter));
            bindScript();
            counter++;

          });
          $("table.order-list").on("click", ".ibtnDel", function(_event) {
            $(this).closest("tr").remove();
            evaluateTotal();
          });
        });

        /* Apend value to all row */
        $("#ok_button").on('click', function() {
          let val = $("#cash_text").val();
          $("#pay_narrat, #acc_narrat").val(val);
        });

Upvotes: 1

Related Questions