I am developing a Ruby on rails project. And in the view file I have some javascript to clone table row on button click. Each table row contains several input text boxes. Now I want to assign the id to a input text box of the newly cloned row dynamically. But I am facing this error:
index:48 Uncaught ReferenceError: item1 is not defined
index is the view name.
My code is:
function insRow()
{
var x=document.getElementById('POITable');
var new_row = x.rows[1].cloneNode(true);
new_row.style.visibility = "visible";
var len = x.rows.length;
new_row_itembox=new_row.cells[0].getElementsByTagName("input");
new_row_itembox.id=<%= ("item"+@counter.to_s) %>; //this line causes the error
//console.log(new_row_itembox);
var inp1 = new_row.cells[1].getElementsByTagName('input')[0];
inp1.id += len;
inp1.value = '';
var inp2 = new_row.cells[2].getElementsByTagName('input')[0];
inp2.id += len;
inp2.value = '';
x.appendChild( new_row );
var sec_last_row=x.rows[x.rows.length-2];
console.log(x.rows.length);
console.log(x.rows[x.rows.length-2]);
sec_last_row.cells[5].innerHTML="<img alt='Icon' src='/assets/minus-icon.png' id='delPOIbutton' onclick='deleteRow(this)'/>";
sec_last_row.cells[6].innerHTML="";
var last_row=x.rows[x.rows.length-1]
last_row.cells[5].innerHTML="<img alt='Icon' src='/assets/add-icon.png' id='addmorePOIbutton' onclick='insRow(this)'/>";
last_row.cells[6].innerHTML="<img alt='Icon' src='/assets/minus-icon.png' id='delPOIbutton' onclick='deleteRow(this)'/>";
}
The above javascript function is for inserting another row on a button click.
In the code, @counter is a rails variable defined in the corresponding controller action.
If I remove that part and assign the id of the input box statically,it working fine.
My html code is:
<body>
<form>
<center>
<div id="POItablediv">
<br/>
<table id="POITable">
<tr>
<th>Item</th>
<th>Brand</th>
<th>UOM</th>
<th>Quantity</th>
<th>Remarks</th>
</tr>
<tr style="visibility:hidden;"> <!-- This is just a dummy row-->
<td><input size=25 type="text"/></td>
<td><input size=25 type="text"/></td>
<td><input size=25 type="text"/></td>
<td><input size=25 type="text"/></td>
<td><input size=25 type="text"/></td>
<td><img alt="Icon" src="/assets/add-icon.png" id="addmorePOIbutton" onclick="insRow()" /></td>
<td><img alt="Icon" src="/assets/minus-icon.png" id="delPOIbutton" onclick="deleteRow(this)"/></td>
</tr>
<tr>
<td><input size=25 type="text"/></td>
<td><input size=25 type="text"/></td>
<td><input size=25 type="text"/></td>
<td><input size=25 type="text"/></td>
<td><input size=25 type="text"/></td>
<td><img alt="Icon" src="/assets/add-icon.png" id="addmorePOIbutton" onclick="insRow()" /></td>
<td><img alt="Icon" src="/assets/minus-icon.png" id="delPOIbutton" onclick="deleteRow(this)"/></td>
</tr>
</table>
</div>
<input type="submit" name="submit_button" onclick="make_hash()">
</center>
</form>
</body>
Change the line
new_row_itembox.id=<%= ("item"+@counter.to_s) %>;
to
new_row_itembox.id='<%= ("item"+@counter.to_s) %>';
^ ^
-------- added these ---------