Form Input Value not updating from script

user982124

I have a text input as follows:

<td><input type="text" class="form-control assetID" autocomplete="off" placeholder="Asset ID" name="assetID[]"></td>

and a script that runs when the first input field is modified. I would like to update the value of the 2nd input when this script runs:

 $this.closest('tr').children('.form-control.assetID').val(assetID);

with the value of the asset variable. I can't get the form input to update here when this script runs and can't see the issue here? I will have a series of similar inputs that are added dynamically so I'm not using an id for these but targeting them by the class.

Here's an example of my table/scripts:

$(document).ready(function() {
  $("#addRow").click(function() {
    var markup = "<tr><td><input type=\"text\" class=\"form-control serialNumber\" autocomplete=\"off\" placeholder=\"Serial Number\" name=\"serialNumber[]\" value=\"\"></td><td><input type=\"text\" class=\"form-control assetID\" autocomplete=\"off\" placeholder=\"Asset ID\" name=\"assetID[]\" value=\"\"></td></td><td class=\"productCode\"></td><td class=\"description\"></td><td class=\"text-center deleteRow\"><span class=\"glyphicon glyphicon-trash\"></span></td></tr>";
    $("#shipmentItems").append(markup);
  });

  // Find and remove selected table rows

  $("#shipmentItems").on("click", ".deleteRow", function() {
    $(this).closest('tr').remove();
  });
});




$(document).ready(function() {
  $(document).on('change', '.form-control.serialNumber', function() {

    var serialNumber = $(this).val();
    //console.log( recid );
    // Create a reference to $(this) here:
    $this = $(this);

    ID = 'ABC123';
    code = 'PC8765';
    description = 'Acme Standard Widget';

    $this.closest('tr').children('.form-control.assetID').val(ID);
    $this.closest('tr').children('.code').html(code);
    $this.closest('tr').children('.description').html(description);

  });
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>


<table id="shipmentItems" class="table table-condensed table-striped table-bordered">
  <thead>
    <th class="text-center" scope="col" width="20%">Serial</th>
    <th class="text-center" scope="col" width="15%">ID</th>
    <th class="text-center" scope="col" width="15%">Code</th>
    <th class="text-center" scope="col" width="45%">Description</th>
    <th class="text-center" scope="col" width="5%"></th>
  </thead>
  <tbody>



    <tr>
      <td><input type="text" class="form-control serialNumber" autocomplete="off" placeholder="Serial Number" name="serialNumber[]" value=""></td>
      <td><input type="text" class="form-control assetID" autocomplete="off" placeholder="Asset ID" name="assetID[]" value=""></td>
      <td class="code"></td>
      <td class="description"></td>
      <td class="deleteRow"><span class="glyphicon glyphicon-trash"></span></td>
    </tr>


  </tbody>
</table>

<button type="button" name="addRow" value="addRow" id="addRow" class="btn btn-primary">Add Asset</button> &nbsp; &nbsp;

When I change the first column I would like the values in the other columns to be updated.

Abdennour TOUMI

Not this

.children('.form-control assetID')

But this

.children('.form-control.assetID')

Explanation: Selecting element with 2 classes requires to not keep space between both classes , also prefixing both classes by .

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

jQuery Script Not Updating Form Input Value

Updating dataGridView value from another form

Not Updating setTimeout() value on click from input box

input value is undefined from form

Updating initialValues prop on Formik Form does not update input value

Updating input value by updating props

Updating a column price in my product table with input received from a form

React updating state in two input fields from form submission

Value of input not updating in jquery

How to get a return value from an input form?

Get form input value from angularjs

Angular Reactive forms - Value from Async call not updating form

Updating the user entered value from a php form to the database

Setting value of input from a jQuery script

Updating Django form based on input

Retrive the value from text input and updating/saving with ajax

Using a value from a file and updating it using a Linux script:

Prevent a Form Input value from being submitted in the form

Form input using Redux Form not updating

get value from form select and then calculate value in an input

Use value from input field where value is a return form JS

AJAX not sending user-input from a form to PHP script

Taking user input from HTML form as a variable for Python script

passing parameter from java script on input html form

Use a value from a script as a default value inside a form field?

State value not updating on form validation

Undefined value when calling an input from a HTML form from PHP

Javascript TextBox input value is not updating

Angular custom form control not updating form value