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

Go To


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">
    <% @brands.each do |brand| %>
        <td><%= link_to, brand_path(brand) %></td>
        <% @model1 = Model.find_by_brand_id( %>
        <td><%= %></td>
        <% @submodel1 = Submodel.find_by_model_id( %>
        <td><%= %></td>
        <% @style1 = Style.find_by_submodel_id( %>
        <td><%= %></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' %>
    <% end %>

2012-04-04 20:45
by Abram


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


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
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