Big Mike
Big Mike

Reputation: 119

JavaScript Cloned Table Rows - reset value

I have a form/table set up that's cloning rows with specific valued names so that they can be processed separately once the form submits. All seems to be well unless you fill out information on the 5th row (the row being cloned) and then it copies all of the data over into the cloned row.

Is there an easy way to reset the values to NULL and 0 during the cloning process?

(I have two rows on bottom that are tallying/totalling each column, hence the tr:nth-last-child(2))

var totalRows = 5;

function addrow() {
  totalRows++;
  var clonedRow = $("table#maintable tr:nth-child(5)").clone(); //this will grab the 5th table row.

  //get the textfield
  var tfName = $("input.name",clonedRow);
  var tfproA = $("input.proA",clonedRow);
  var tfproB = $("input.proB",clonedRow);
  var tfproC = $("input.proC",clonedRow);
  var tfproD = $("input.proD",clonedRow);
  var tfproE = $("input.proE",clonedRow);
  var tfproF = $("input.proF",clonedRow);
  var tfproG = $("input.proG",clonedRow);
  var tfproH = $("input.proH",clonedRow);
  var tfproI = $("input.proI",clonedRow);
  var tfproJ = $("input.proJ",clonedRow);
  var tfproK = $("input.proK",clonedRow);
  var tfproL = $("input.proL",clonedRow);
  var tfproM = $("input.proM",clonedRow);
  var tfproN = $("input.proN",clonedRow);

  //change their names
  tfName.attr("name","name"+totalRows);
  tfproA.attr("name","proA"+totalRows);
  tfproB.attr("name","proB"+totalRows);
  tfproC.attr("name","proC"+totalRows);
  tfproD.attr("name","proD"+totalRows);
  tfproE.attr("name","proE"+totalRows);
  tfproF.attr("name","proF"+totalRows);
  tfproG.attr("name","proG"+totalRows);
  tfproH.attr("name","proH"+totalRows);
  tfproI.attr("name","proI"+totalRows);
  tfproJ.attr("name","proJ"+totalRows);
  tfproK.attr("name","proK"+totalRows);
  tfproL.attr("name","proL"+totalRows);
  tfproM.attr("name","proM"+totalRows);
  tfproN.attr("name","proN"+totalRows);

  $("table#maintable tr:nth-last-child(2)").before(clonedRow); //add the row back to the table before the last 2nd to last row
  console.log('done')
}

Upvotes: 1

Views: 146

Answers (1)

JoshBerke
JoshBerke

Reputation: 67108

You can chain together a call to val()

tfName.attr("name","name"+totalRows).val(0) or

tfName.attr("name","name"+totalRows).val('')

Upvotes: 1

Related Questions