Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table with data and in last column in each row is fa-trash-o icon which needs just to be shown when its hovered and also to delete that row when is clicked on it.
I made part for deleting row working and also for disappearing, but both function in combination doesn't work

What I have tried:

Here is my code:
HTML
<tr>
    <td class="tg-98kr">Libby Folfax</td>
    <td class="tg-98kr">folfax2014@example.com</td>
    <td class="tg-98kr">City</td>
    <td class="tg-98kr">Sometimes</td>
    <td class="tg-98kr">Mon, Tue, Wed</td>
    <td class="tg-98kr">13/08/2018 11:29AM</td>
    <td class="tg-98kr"><a href=""><button type="button" onclick="deleteRow(this)" class=" w3-button" style=" hover:display"></button></a></td>
  </tr>
  <tr>
    <td class="tg-q3hw">Nick Dean</td>
    <td class="tg-q3hw">nickd@example.com</td>
    <td class="tg-q3hw">City</td>
    <td class="tg-q3hw">Always</td>
    <td class="tg-q3hw">Fri, Sat</td>
    <td class="tg-q3hw">13/08/2013 11:29AM</td>
    <td class="tg-q3hw"><a ><button type="button" onclick="deleteRow(this)"  class=" w3-button " style=" hover:display">^__i class="fa fa-trash-o"></button></a></td>
  </tr>
</table>
</section>


JavaScript
<script>
function deleteRow(btn) {
  var row = btn.parentNode.parentNode;
  row.parentNode.removeChild(row);
}
</script>


CSS
table a{
   display: none;
}
table tr:hover a{
    display: block;
}
Posted
Updated 17-Feb-18 12:47pm

1 solution

Well... I don't know why you're wrapping your buttons in anchor tags, nor the inline styles, but they don't help as far as this is concerned - anyway this works for me:
CSS
<style type="text/css">
table input{
   display: none;
}
table tr:hover input{
    display: block;
}
    </style>
HTML
<table>
<tr>
    <td class="tg-98kr">Libby Folfax</td>
    <td class="tg-98kr">folfax2014@example.com</td>
    <td class="tg-98kr">City</td>
    <td class="tg-98kr">Sometimes</td>
    <td class="tg-98kr">Mon, Tue, Wed</td>
    <td class="tg-98kr">13/08/2018 11:29AM</td>
    <td class="tg-98kr"><input type="button" onclick="deleteRow(this)" class="w3-button"  value="delete" /></td>
  </tr>
  <tr>
    <td class="tg-q3hw">Nick Dean</td>
    <td class="tg-q3hw">nickd@example.com</td>
    <td class="tg-q3hw">City</td>
    <td class="tg-q3hw">Always</td>
    <td class="tg-q3hw">Fri, Sat</td>
    <td class="tg-q3hw">13/08/2013 11:29AM</td>
    <td class="tg-q3hw"><input type="button" onclick="deleteRow(this)"  class="w3-button" value="delete" /></td>
  </tr>
</table>
JavaScript
<script type="text/javascript">
       function deleteRow(btn) {
          var row = btn.parentNode.parentNode;
          row.parentNode.removeChild(row);
       }
    </script>
 
Share this answer
 
Comments
Member 13682154 18-Feb-18 2:48am    
Thank you for a help, but it still doesn't work for me. There shouldn't be anything written on the button, except icon
Also when I am not hovering the table it still showing value of the button.
It needs to show just when I am hovering that row.
A_Griffin 18-Feb-18 3:46am    
Well it's not hard to replace the button the an image - but what can I say? It works for me in the manner you want, in IE, Firefox and Chrome: it only shows on each row as that row is hovered over.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900