Search code examples
jqueryfindclone

jquery clone last table row with excludes


I have an input table which it's last row is:

    <tr>
        <td>
           <input type="hidden" value="" readonly="" size="3" name="id[]">
       </td>
<td>
      <input type="hidden" value="" readonly="" size="3" name="fkcommand[]">
      </td>
<td>
<select name="type[]">
<option value="C" selected="selected">C</option>
<option value="P">P</option></select>
</td>
<td>
  <input type="input" value="1200" size="6" name="strike[]">
</td><td>
      <input type="input" value="-2" size="6" name="quantity[]">
    </td>
    </tr>

and I want to clone this input line, empty it's values except for the hidden value fkcommand and append it the end of the table

I am using this:

var row = $(this).parent().find('table tbody tr:last');
row.clone().find('input').val('').end().insertAfter(row).find('[name=fkcommand[]]').val('222');

which doesn't add the value '222' to fkcommand


Solution

  • Possibly the problem is in the selector. It is better to quote the attribute values:

    .find("[name='fkcommand[]']")
    

    However, I suggest you to rewrite your code in such manner:

    row.clone().insertAfter(row).find("input").each(function() {
        this.value = this.name == "fkcommand[]" ? "222" : "";
    });