I'm building out a front end entry form with a Natrix and everything is coming along fine. However when users create a new row each new row needs to be unique in order to be saved. This is where I'm stuck.
Simplified HTML:
<div class="matrixblock">
<p>
block id: new_0
</p>
<div class="col-sm-6">
<input type="text" name="fields[staffProfiles][new_0][fields][employeeName]" value="" placeholder="Full Name">
</div><!-- /.col-sm-6 -->
<div class="col-sm-6">
<input type="text" name="fields[staffProfiles][new_0][fields][jobTitle]" value="" placeholder="Job Title">
</div><!-- /.col-sm-6 -->
<a href="" class="remove-row"><i class="fa fa-times"></i></a>
</div><!-- /.matrixblock -->
JS:
var blocknum = 0;
// add a new person
$('#addPerson').click(function(e){
e.preventDefault();
console.log('clicked addPerson');
//iterate blocknumber
blocknum = blocknum+= 1;
console.log('blocknum is ' + blocknum);
var newPerson = "<div class='newperson'></div>";
$('.newBlock').append(newPerson);
$('.newBlock .newperson:last-child').load('/store/newperson');
// replace all instances of new_0 with new1, new2 etc.. as rows added
// this line doesn't do anything, why?
$('.newBlock .newperson:last-child').replace('new_0','new' + blocknum);
})
console.log('blocknum is ' + blocknum);
is showing that the blocknum is increasing with each row. the Replace in the last line of javascript is just not doing anything.
The new person row gets added via ajax correctly but each time it's added [new_0] needs to be unique, but nothing I'm trying is working.