Liad
Liad

Reputation: 33

Using Coffeescript to hide a <div> in rails

I have a list of items that I render via a call to a partial

render @items

each item is placed inside a div and has a JS 'disable' link

<div id="item_id_<%=item.id%>
   <%=item.name>
   <%= link_to disable_item_path(item), :remote => true %>
</div>

Inside the controller, I change the flag of the item in the database to False, and want to re-render the page and remove the item from the list.

I have been doing so by the following code in disable.js.coffee

$('#items_list').html("<%= escape_javascript(render(:partial => @items)) %>")

My question: I would like simply to hide the specific item DIV, and not re-render all the items on the page (why? because I think it's better coding).

How do I do that? I tried passing @item_id to the coffeescript, and doing something like

$('item_id'+@item_id).hide

but from reading around here it seems like it's the wrong way of doing so.

Thanks!

Upvotes: 0

Views: 2285

Answers (2)

Dylan Markow
Dylan Markow

Reputation: 124419

To simply hide the div, put this in your disable.js.coffee file:

$("#item_id_<%= item.id %>").hide()

Upvotes: 1

Jonathan
Jonathan

Reputation: 11494

Why not have a parent #items div with each child .item class, then reference by $('#items > .item').hide or something like that.

At the moment you have

<div id="item_id_1">...</div>
<div id="item_id_2">...</div>
<div id="item_id_3">...</div>

And so on.

Instead, you can have:

<div id="items">
  <div class="item">...</div>
  <div class="item">...</div>
  <div class="item">...</div>
</div>

Upvotes: 0

Related Questions