I have a list of items with some details that I would like to reveal upon clicking a show/hide details link. after some experimenting, I've come across two issues.
I'm having trouble making it so that the show/hide link only reveals the div of a specific item. right now, it is revealing all of the divs when clicking any of the show/hide links.
Also, there has got to be a more elegant way to do this without doing a loop to generate a bunch of javascript. I looked into effect.multiple, but I'm not sure how to use it in this context.
some insight or pointing in the general direction would be greatly appreciated! Thanks!
here's the code for reference.
<script type="text/javascript">
function show_details() {
<% @posts.each do |b| %>
Effect.BlindDown('details_<%= b.id %>', {duration:0.3});
$('hide_details_link_<%= b.id %>').style.display = 'inline';
$('show_details_link_<%= b.id %>').style.display = 'none';
<% end %>
}
function hide_details() {
<% @posts.each do |b| %>
Effect.BlindUp('details_<%= b.id %>', {duration:0.3});
$('hide_details_link_<%= b.id %>').style.display = 'none';
$('show_details_link_<%= b.id %>').style.display = 'inline';
<% end %>
}
</script>
<ul id="posts">
<% @posts.each do |b| %>
<li>
<div id="show_details_link_<%= b.id %>" style="display:inline;">
<%= link_to_function "show details", 'show_details()' %>
</div>
<div id="hide_details_link_<%= b.id %>" style="display:none;">
<%= link_to_function "hide details", 'hide_details()' %>
</div>
<div id="details_<%= b.id %>" style="display:none;">
<p> <%= b.comments %></p>
</div>
</li>
<li><%= link_to b.title, {:action => 'show', :id => b.id} -%></li>
<% end %>
</ul>