Reputation: 2374
please i am trying to add an onclick event to my button on rails 5, when click this the function is to add row to a table but its not adding row as excepted, i need help please.
**addRow function**
function addrow(count){
var table = document.getElementById("result_table");
var row = document.getElementById("result_table").lastChild;
var clone = row.cloneNode(true);
for (i=0; i<count; i++)}{
table.appendChild(clone);
}
}
and the above function is in a file called controlMenus.js which is placed in C:\Users\UserName\Documents\department\app\assets\javascripts rails project directory.
**The view**
<div = "row">
<div class = "col-8">
<div class = "col-1"></div>
<div class = "col-1 no-padding">
<%= render 'layouts/lecturer_menu' %>
</div>
<div class = "col-5 no-padding margin-left">
<div class = "trans-background no-padding">
<h5 class = "pad-left centered black-font">ENTER RESULT</h5>
<table id = "result_table" border = "1" style="width: 100%;
align: center;">
<th>S/N</th>
<th>MATNO</th>
<th>NAME</th>
<th>COURSE TITLE</th>
<th>COURSE CODE</th>
<th>GRADE</th>
<th>POINT</th>
<tr>
<td>1</td>
<td>HELLO</td>
<td>HELLO</td>
<td>HELLO</td>
<td>HELLO</td>
<td>HELLO</td>
<td>HELLO</td>
</tr>
<% for i in([email protected]_i)do %>
<tr>
<td><%= i+1 %></td>
<td>HELLO</td>
<td>HELLO</td>
<td>HELLO</td>
<td>HELLO</td>
<td>HELLO</td>
<td>HELLO</td>
</tr>
<% end %>
</table>
<%= form_for :rows, url: {controller: "lectureractivities",
action: "addrow"} do |f| %>
<%= f.number_field :row, {min: 1, class: "fixed-width"} %>
<%= submit_tag :addrows, onclick: "addrows();" %>
<% end %>
</div>
</div>
<div class = "col-1"></div>
</div>
</div>
Upvotes: 1
Views: 5804
Reputation: 2927
What does not working mean? What have you done to debug? Have you confirmed that the js file is loading? Have you logged anything to the console to ensure that your js function variables are what you expect them to be? For starters, there are a number of errors in your html, the most glaring is in this line
<%= submit_tag :addrows, onclick: "addrows();" %>
You call a function "addrows", but your function is defined as "addrow". Try changing it to
<%= f.submit_tag :addrows, onclick: "addrow();" %>
Upvotes: 3