Search code examples
cssasp.netfont-awesomeonmouseoveronmouseout

font-awesome icon - onmouseover event change color


I have an ASP.NET gridview control, with a font-awesome trash icon in the end column.

On mouseover I want the color to change to red, and back again to default onmouseout.

Here is the template field in the asp.net gridview:

<asp:TemplateField HeaderText="Delete?" ItemStyle-HorizontalAlign="Center">
    <ItemTemplate>
        <span ID="lnkDelete" style="border:none" onclick="return confirm('Are you sure you want to delete this Performance Review?')"
                    runat="server" ItemStyle-CssClass="fa fa-trash-0" 
                onmouseover="this.style='color:red;'"    
                CommandName="Delete">
                <i  class="fa fa-trash-o" onmouseover="script:this.style='color:red; font-size:24px'"  onmouseout="script:this.style='color:black; font-size:24px'"
                    style="font-size:24px;"></i> 
            </span>

    </ItemTemplate>
</asp:TemplateField>

and in IE it is rendered as

<span id="ctl00_m_g_10488b48_1486_45be_8a9c_efc307c0cb4b_ctl00_grdPR_ctl04_lnkDelete" style="border: currentColor;" 
onclick="return confirm('Are you sure you want to delete this Performance Review?')" CommandName="Delete" ItemStyle-CssClass="fa fa-trash-0">
<i class="fa fa-trash-o" style="font-size: 24px;" onmouseover="script:this.style='color:red; font-size:24px'"></i> 
                    </span>

but I am obviously getting it wrong as nothing changes when I hover over the trash-can icon.

I have also tried using the css hover style in my page, but again, nothing happens:

<style>
.fa fa-trash-o{
  color: black
}
.fa fa-trash-o:hover{
  color:red;
}
</style>

so where am I going wrong?


Solution

  • You can achieve this by only CSS. The problem is you are using wrong selector in the css. The fa and fa-trash-o classes are in the same element. So you need to remove the space between fa and fa-trash-o and put . before them as they are classes

    .fa.fa-trash-o {
      color: black
    }
    
    .fa.fa-trash-o:hover {
      color: red;
    }
    <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
    <i class="fa fa-trash-o"></i>