Drop Drop - 7 months ago 17
Javascript Question

how to add in placeholder sequential addition number?

When i click add string btn > i clone first tr whith input in table and add in this table, and i need add +1 number in placeholder copied element.How determine last placeholder. Before copy and add in new copied placeholder/
this html table

<div class="matrix_a_cover">
<table class="matrix_a brackets" id="matrix_a">

<tr>
<td class="str_inp">
<input type="text" placeholder="a1,1">
</td>
<td class="str_inp">
<input type="text" placeholder="a1,2">
</td>
</tr>

<tr>
<td class="str_inp">
<input type="text" placeholder="a2,1">
</td>
<td class="str_inp">
<input type="text" placeholder="a2,2">
</td>
</tr>

<tr>
<td class="str_inp">
<input type="text" placeholder="a3,1">
</td>
<td class="str_inp">
<input type="text" placeholder="a3,2">
</td>
</tr>

<tr>
<td class="str_inp">
<input type="text" placeholder="a4,1">
</td>
<td class="str_inp">
<input type="text" placeholder="a4,2">
</td>
</tr>


</table>
</div>


this code copy element

$('.add_str').click(function(){
if ($("#check_mtrx_a").prop("checked")){
$('.matrix_a tr:first').clone().appendTo('.matrix_a');
}else if($("#check_mtrx_b").prop("checked")) {
$('.matrix_b tr:first').clone().appendTo('.matrix_b');
}
});


and if need example how work this all link serjo96.github.io/matrix

Answer

Trigger the following function when you update the table

function placeHold() {
$('tr').each(function(i,v){
  $(v).find('td').each(function(x,d){
    $(d).find('input').attr('placeholder','a'+i+','+x)
  });
});

Note: this will start from a0,0

Comments