Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript add another row in atable for unique id showing NAN value for id
    primarykey
    data
    text
    <p>I have a html table. Inside that my markup is like this</p> <pre><code>&lt;table id="invoices-product-detail"&gt; &lt;tbody id="tableToModify"&gt; &lt;?php $i=1; while($i&lt;2){ ?&gt; &lt;tr&gt; &lt;td&gt; &lt;input type="hidden" name="prdid[]" id="prd_id&lt;?php echo $i; ?&gt;" /&gt; &lt;/td&gt; &lt;td&gt; &lt;input type="text" name="prdcost[]" id="prd_cost&lt;?php echo $i; ?&gt;" value="0" disabled="disabled" style="color:#333;" /&gt; &lt;/td&gt; &lt;td&gt; &lt;input type="text" name="prdquentity[]" id="prd_quentity&lt;?php echo $i; ?&gt;" tabindex="&lt;?php echo 3+($i*5);?&gt;" onKeyUp="calprice1(this.value,'&lt;?php echo $i; ?&gt;');" value="1" /&gt; &lt;/td&gt; &lt;td&gt; &lt;input type="text" name="prddamount[]" tabindex="&lt;?php echo 5+($i*5);?&gt;" readonly="true" id="prd_damount&lt;?php echo $i; ?&gt;" onKeyUp="calprice2(this.value,'&lt;?php echo $i; ?&gt;');" /&gt; &lt;/td&gt; &lt;td id="readonly"&gt; &lt;input readonly="readonly" name="prdprice[]" id="prd_price&lt;?php echo $i; ?&gt;" value="0" /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;?php $i++; } ?&gt; &lt;/tbody&gt; &lt;/table&gt;&lt;!--#invoices-product-detail--&gt; </code></pre> <p>Now under that I have a button called add a <code>new line</code></p> <pre><code>&lt;input type="button" onClick="createRow();" value="Add a new line"&gt;&lt;/span&gt; </code></pre> <p>in js file I have the <code>createRow() function</code> like this</p> <pre><code>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 = "&lt;input type=\"hidden\" name=\"prdid[]\" id=\"prd_id"+newlineno+"\" /&gt;&lt;input type=\"text\" name=\"prdname[]\" id=\"prd_name"+newlineno+"\"; col2.innerHTML = "&lt;input type=\"text\" disabled=\"disabled\" name=\"prddesc[]\" id=\"prd_desc"+newlineno+"\" /&gt;"; col3.innerHTML = "&lt;input type=\"text\" disabled=\"disabled\" name=\"prdcost[]\" id=\"prd_cost"+newlineno+"\" value=\"0\" /&gt;"; col4.innerHTML = "&lt;input type=\"text\" name=\"prdquentity[]\" id=\"prd_quentity"+newlineno+"\" onKeyUp=\"calprice1(this.value,'"+newlineno+"');\" value=\"1\" /&gt;"; col5.innerHTML = "&lt;select name=\"prddtype[]\" class=\"discount-type\" id=\"prd_dtype"+newlineno+"\" &gt;"+document.getElementById("prd_dtype0").innerHTML+"&lt;/select&gt;"; col6.innerHTML = "&lt;input type=\"text\" name=\"prddamount[]\" id=\"prd_damount"+newlineno+"\" onKeyUp=\"calprice2(this.value,'"+newlineno+"');\" /&gt;"; col7.innerHTML = "&lt;input type=\"text\" name=\"prdprice[]\" id=\"prd_price"+newlineno+"\" class=\"price-input-text\" disabled=\"disabled\" value=\"0\" /&gt;"; var table = document.getElementById("tableToModify"); // find table to append to table.appendChild(row); // append row to table } </code></pre> <p>Now here everything is fine. As per js I can easily add another row. But when I checked the id of input fields I saw that the first row(default one) is showing <code>&lt;input type="text id="prd_name1"&gt;</code> which one is fine. But when I clicked to the add a new line and the new created line id was <code>&lt;input type="text" id="prd_nameNaN"&gt;</code>. But here I want the id should be like <code>&lt;input type="text" id="prd_name2"&gt;</code> and for 3rd row <code>&lt;input type="text" id="prd_name3"&gt;</code> like so on. Here for every new created row the number of row will be added to every field id. Here can someone kindly tell me why this issue is here? Any help and suggestions are highly appreciable. Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload