How to modify cloned field form value and id inside table row?

Idham Choudry

I have 2 fields in my form that I want to clone using jQuery, but the selecting html table structure got me confused on how to change the id and the value of my form field and also the label text. Here's the form field structure

<table>
 <tbody>
    <tr id="attribute-name">
      <td class="label"><label for="listing_qty">quantity</label></td>
      <td class="value">
        <input id="listing_qty" name="field_name[]" value="quantity" class="required-entry disabled attribute-name input-text" readonly="1" type="text">
      </td>
    </tr>
    <tr id="attribute-custom">
      <td class="label"></td>
      <td class="value">
        <input id="listing_custom_field" name="custom_field[]" value="" placeholder="Custom Attribute Field" type="text" class=" input-text">
      </td>
    </tr>
 </tbody>
</table>
backpackcoder

You will need to clone the entire element and then update the id's, values, and text in the cloned element before inserting.

function appendClonedFormInput(el,
  newId,
  newInputId,
  newLabelText,
  newName,
  newValue) {
  // Clone and update id
  var $cloned = $(el)
    .clone()
    .attr('id', newId);
  // Update label
  $cloned.find('label')
    .attr('for', newInputId)
    .text(newLabelText);
  // Update input
  $cloned.find('input')
    .attr('id', newInputId)
    .attr('name', newName)
    .val(newValue);
  return $cloned.insertAfter(
    $('input').last().parents('tr'));
}


appendClonedFormInput('#attribute-name',
  'new-attribute-id',
  'new-inp-id',
  'New Label',
  'new_field',
  'new value');


appendClonedFormInput('#attribute-custom',
  'new-custom-id',
  'new-custom-inp-id',
  'New Custom',
  'new_custom',
  'new custom value');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tbody>
    <tr id="attribute-name">
      <td class="label">
        <label for="listing_qty">quantity</label>
      </td>
      <td class="value">
        <input id="listing_qty" name="field_name[]" value="quantity" class="required-entry disabled attribute-name input-text" readonly="1" type="text">
      </td>
    </tr>
    <tr id="attribute-custom">
      <td class="label"></td>
      <td class="value">
        <input id="listing_custom_field" name="custom_field[]" value="" placeholder="Custom Attribute Field" type="text" class=" input-text">
      </td>
    </tr>
  </tbody>
</table>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to read value of a input field in JSP form through a Servlet using id of that field?

How to properly remove the dynamically cloned row from a table

How to modify the n following value of a selected row

Angular 6 material table adds row(form field) with last entered value

How to copy table row and append after modify input value

How to remove a cloned row

How do I reference an attribute inside a table to a value inside a new row within the same table

How to increment an id based on a field having a certain value going row by row

How to display form using formGroup inside table row in angular 8

How do I delete rows in one table where the ID matches another table row where a field is a certain value?

How to get row count with same field value as from row with provided id?

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

How to find a div element inside the row table and return the value?

how to set default value for select field inside a form

How to set row id value after form editing in free jqgrid

how to change Id of textbox inside a cloned div

jQuery How To Modify html of a cloned element in memory

How to get table row by id and then change it's value / html

How do I need to modify this MERGE statement, for a mass update of one field based on the value of a field in another table?

js how to change id inside cloned form with its own new id

how to get the id value in each row from the append table in JavaScript

MySQL - How to select rows in a table where id value is in a comma delimited field in another table?

How do I modify an object value in a cloned array of objects in Javascript

How to modify every value of a row in a table using shell function?

How to Default a Form Field Value from a Table Field Value? (MS Access)

How do I dynamically add an input field into a cell inside my html table row in javascript?

How to take a table row's coloumn to form input field

How to take a table row's column to form input field

How to modify a struct field inside closure via a raw pointer?