I am new to JavaScript and rails. I am building a site in ruby on rails and I am trying to use JavaScript in a view. I need to hide and show some elements of a table I have some script task down and it is only working for the first row of the table. is there a better way of doing this thank you for any help. i need each button to show or hide the corresponding element in the table
application.html
<!DOCTYPE html>
<script>
function changeColor(newColor) {
document.getElementById("para1").style.display = "none"
document.getElementById("para2").style.display = "none"
document.getElementById("para3").style.display = "none"
document.getElementById("para4").style.display = "block"
document.getElementById("para5").style.display = "none"
}
function show(newColor) {
document.getElementById("para1").style.display = "block"
document.getElementById("para2").style.display = "block"
document.getElementById("para3").style.display = "block"
document.getElementById("para4").style.display = "none"
document.getElementById("para5").style.display = "block"
}
</script>
<html>
<head>
<title>VincentricData</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= stylesheet_link_tag "defaults", media: "all", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
index.html
<h1>Listing vehicles</h1>
<table>
<thead>
<tr>
<th>Year</th>
<th>Make</th>
<th>Model</th>
<th>Msrp</th>
<th>Rating</th>
<th>Total operating costs</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @vehicles.each do |vehicle| %>
<tr>
<td><%= vehicle.year %></td>
<td><%= vehicle.make %></td>
<td><%= vehicle.model %></td>
<td><div id="para1" ><%= vehicle.msrp %></div></td>
<td><div id="para2" ><%= vehicle.rating %></div></td>
<td><div id="para3" ><%= vehicle.total_operating_costs %></div></td>
<td><div id="para4" ><button onclick="show('#00FFFF');">Show</button></div></td>
<td><div id="para5" ><button onclick="changeColor('#00FFFF');">hide</button></div></td>
<td><%= link_to 'Edit', edit_vehicle_path(vehicle) %></td>
<td><%= link_to 'Destroy', vehicle, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Vehicle', new_vehicle_path %>