I have an html table. Inside that my markup looks like this
<table id="invoices-product-detail"> <tbody id="tableToModify"> <?php $i=1; while($i<2){ ?> <tr> <td> <input type="hidden" name="prdid[]" id="prd_id<?php echo $i; ?>" /> </td> <td> <input type="text" name="prdcost[]" id="prd_cost<?php echo $i; ?>" value="0" disabled="disabled" style="color:#333;" /> </td> <td> <input type="text" name="prdquentity[]" id="prd_quentity<?php echo $i; ?>" tabindex="<?php echo 3+($i*5);?>" onKeyUp="calprice1(this.value,'<?php echo $i; ?>');" value="1" /> </td> <td> <input type="text" name="prddamount[]" tabindex="<?php echo 5+($i*5);?>" readonly="true" id="prd_damount<?php echo $i; ?>" onKeyUp="calprice2(this.value,'<?php echo $i; ?>');" /> </td> <td id="readonly"> <input readonly="readonly" name="prdprice[]" id="prd_price<?php echo $i; ?>" value="0" /> </td> </tr> <?php $i++; } ?> </tbody> </table>
Now under that there is a button called add new line
<input type="button" onClick="createRow();" value="Add a new line"></span>
In js file I have createRow() function , like this
function createRow() { var newlineno = document.forms[0].elements["prdprice[]"].length; newlineno++; var row = document.createElement('tr'); var col1 = document.createElement('td'); var col2 = document.createElement('td'); var col3 = document.createElement('td'); var col4 = document.createElement('td'); var col5 = document.createElement('td'); var col6 = document.createElement('td'); var col7 = document.createElement('td'); row.appendChild(col1); row.appendChild(col2); row.appendChild(col3); row.appendChild(col4); row.appendChild(col5); row.appendChild(col6); row.appendChild(col7); col1.innerHTML = "<input type=\"hidden\" name=\"prdid[]\" id=\"prd_id"+newlineno+"\" /><input type=\"text\" name=\"prdname[]\" id=\"prd_name"+newlineno+"\"; col2.innerHTML = "<input type=\"text\" disabled=\"disabled\" name=\"prddesc[]\" id=\"prd_desc"+newlineno+"\" />"; col3.innerHTML = "<input type=\"text\" disabled=\"disabled\" name=\"prdcost[]\" id=\"prd_cost"+newlineno+"\" value=\"0\" />"; col4.innerHTML = "<input type=\"text\" name=\"prdquentity[]\" id=\"prd_quentity"+newlineno+"\" onKeyUp=\"calprice1(this.value,'"+newlineno+"');\" value=\"1\" />"; col5.innerHTML = "<select name=\"prddtype[]\" class=\"discount-type\" id=\"prd_dtype"+newlineno+"\" >"+document.getElementById("prd_dtype0").innerHTML+"</select>"; col6.innerHTML = "<input type=\"text\" name=\"prddamount[]\" id=\"prd_damount"+newlineno+"\" onKeyUp=\"calprice2(this.value,'"+newlineno+"');\" />"; col7.innerHTML = "<input type=\"text\" name=\"prdprice[]\" id=\"prd_price"+newlineno+"\" class=\"price-input-text\" disabled=\"disabled\" value=\"0\" />"; var table = document.getElementById("tableToModify");
Now everything is all right. By js, I can easily add another line. But when I checked the identifier of the input fields, I saw that the first line (by default) shows <input type="text id="prd_name1"> which one is fine. But when I clicked to add a new line, and a new one the generated line identifier was <input type="text" id="prd_nameNaN"> . But here I want the identifier to be <input type="text" id="prd_name2"> and for the third line <input type="text" id="prd_name3"> , for example, the case. Here, for each newly created line the number of lines will be added to each identifier field. There may be someone kindly tell me why this question zdes ? Any help and suggestions are very noticeable. Thanks
source share