Use dom_id to remove table row with jquery .remove()

Go To StackoverFlow.com

1

I have the following code to generate a table listing brand/model/submodel/style for a group of products. I would like to remove an entire row from the table when the destroy button is clicked, but I can't figure out how to do it using dom_id and jquery's .remove()

Thus far I have tried a few variations on $('#<%= dom_id(@brand) %>').remove(); with no success. Thanks!

<div class = "span8">
<table class="table table-striped" id="devices">
  <thead>
    <tr>
      <th>Brand</th>
      <th>Model</th>
      <th>Submodel</th>
      <th>Style</th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <% @brands.each do |brand| %>
      <tr>
        <td><%= link_to brand.name, brand_path(brand) %></td>
        <% @model1 = Model.find_by_brand_id(brand.id) %>
        <td><%= @model1.name %></td>
        <% @submodel1 = Submodel.find_by_model_id(@model1.id) %>
        <td><%= @submodel1.name %></td>
        <% @style1 = Style.find_by_submodel_id(@submodel1.id) %>
        <td><%= @style1.name %></td>
        <td style = "border:none;">
          <%= link_to 'Edit', edit_brand_path(brand), :class => 'btn btn-mini' %>
          <%= link_to 'Destroy', brand_path(brand), :method => :delete, :remote=>true, :confirm => 'Are you sure?', :class => 'btn btn-mini btn-danger' %>
        </td>
      </tr>
    <% end %>


  </tbody>
</table>
</div>
2012-04-04 20:45
by Abram


4

The $('#blah') syntax represents an ID selector, which selects an element based on its id attribute. From the code you've provided, none of your table rows (<tr> elements) have an id attribute, so I'm not sure what you're expecting to happen. I suspect what you want is:

<tr id="<%= dom_id(@brand) %>">

Then $('#<%= dom_id(@brand) %>').remove(); should work.

2012-04-04 20:49
by Anthony Grist
Yeah, this is the kind of thing that happens when you work with an online tutorial and suit it to your needs without fully understanding all the concepts. I figured it would be something like this. Will try your solution after work and give you the tick if it works.. as you were first in - Abram 2012-04-04 21:13
Also there is a gem called recordtaghelper which will allow you to replace your loop code with <%= content_tag_for(:tr, @brands) do |brand| %> and it renders the wrapping <tr>s with the correct id and class - amoebe 2016-08-29 23:13


0

I assume the destroy button is inside the tr then you can do the following,

//$(this) is destroy button assuming it is inside the same tr
$(this).closest('tr').remove();
2012-04-04 20:49
by Selvakumar Arumugam
Thanks, I'll try this too.. as it seems the simplest solution - Abram 2012-04-04 21:13
Afraid this one didn't do anything for me - Abram 2012-04-05 06:50
Ads