Jquery – Change cloned hidden field value

jquery

I am cloning a table row using JQuery and, amongst other things, the row contains a hidden field. I want to change the value of the hidden field when I clone the row.

The form field:

<input type="hidden" name="items.Index" id="items.Index" value="0" />

The JQuery:

var id = document.getElementById("id").value;
var newId = parseInt(id) + 1;
var clonedRow = $("#myTable tr:last").clone();
$("#items.Index", clonedRow).attr({ "value": newId });
$("#myTable").append(clonedRow);

I ahve also tried $("#items.Index", clonedRow).val(newId); in place of $("#items.Index", clonedRow).attr({ "value": newId }); I have other items in the table row which are being successfully manipulated but the value of this form field never changes.

Any ideas?

Best Answer

#items.index is looking for the element with id "#items.index" which doesn't exist in your example, and I think it's an invalid id and this would yield multiple elements in the same page with the same id (which isn't valid).

You might want to try:

$(clonedRow).find("input:hidden").val(newId);