tags:

views:

49

answers:

2

I need to change a link image on mouse hover. I used the following code but does not work. Perhaps the problem is with the image source but I still cannot find what's wrong with he code.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; 

<html xmlns="http://www.w3.org/1999/xhtml"&gt; 
<head><title>Edit Document</title> 

<script src="../Scripts/jquery-1.3.2.js" type="text/javascript"></script> 
<script type="text/javascript"> 

        $(document).ready(function () {
        $("#lnkEdit").hover(
        function () {
            this.src = this.src.replace("../images/edit_off.gif", "../images/edit_on.png");
        },
        function () {
            this.src = this.src.replace("../images/edit_on.png", "../images/edit_off.gif");
        }
        );
    });

</script> 
</head> 
<body> 
<form method="post" action="Hover4.aspx" id="form1"> 
<div class="aspNetHidden"> 
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJNTkwNjIyODQxZGR6kotcsbIsWRfokZrzasCtfPi0dxz4MBWWh9VxSJ6R0Q==" /> 
</div> 

<div> 
    <a id="HyperLink1" href="Hover4.aspx"><img src="../images/edit_off.gif" alt="HyperLink" /></a> 
</div> 
</form> 

Thanks in advance.

+3  A: 
  1. You are referencing #lnkEdit in your code but there is no element with that ID on the page
  2. You don't need replace or any special functions, you can simply assign the new image source.

Try something like this:

JavaScript (updated as per comments)

$(document).ready(function () {
  $('#your_table_id > a').hover(
    function () {
        $('img', this).attr('src', '../images/edit_on.png');
    },
    function () {
        $('img', this).attr('src', '../images/edit_off.png');
    }
  );
});
casablanca
Yes, you are right. I overlooked item 1.I used your code but still does not work change on hover.
NinjaCoder8
I'm sorry, I missed the `#` for `MyImage`. It's fixed now.
casablanca
Thanks for the code. It works if I use an image button but still does not work with the hyperlink. The hyperlink runs on the server side <asp:HyperLink ID="HyperLink1" runat="server" ImageUrl="~/images/Edit_Off.gif">HyperLink</asp:HyperLink>and does not let me use the id="MyImage" because it already uses id="Hyperlink1"Any other suggestion?Thanks for your help.
NinjaCoder8
I've updated the code so that it doesn't need the image ID - it directly references the `<img>` tag inside the link now.
casablanca
Your code works. I failed to mention, however, that I wanted to have the the link on a datalist. When I use your code, it replaces the first link on the table but the other remaining links do not change when I hover over them. Sorry for not mentioning that I wanted to have a list with the links rather than just one link.
NinjaCoder8
Updated. Just replace your table's ID in the code and it will work for all links in the table.
casablanca
Still does not change the image. I pasted your code with the table Id: $('#ctl00_ContentPlaceHolder1_dlOpenRequests > a').hover( function() { $('img', this).attr('src', '../images/Edit_On.png'); }, function() { $('img', this).attr('src', '../images/Edit_Off.png'); });
NinjaCoder8
I tried it myself and it works. We will have to look at your table structure to find out what might be going wrong.
casablanca
I will post the code but might have to modify the original posted script. Is that OK with you?
NinjaCoder8
Sure, go ahead.
casablanca
A: 

credits must go to @casablanca, this should work:

$(document).ready(function () {
  $.each($('#your_table_id > a'), functio(i, item) {
    $(item).hover(
    function () {
        $('img', this).attr('src', '../images/edit_on.png');
    },
    function () {
        $('img', this).attr('src', '../images/edit_off.png');
    });
  });

});
GerManson
You don't even need to wrap it in `each()`: most jQuery functions apply to all matched elements anyway.
casablanca
I used the code and it does not change a single image. Below is the change: $(document).ready(function() { $.each($('#ctl00_ContentPlaceHolder1_dlOpenRequests > a'), function(i, item) { $(item).hover( function () { $('img', this).attr('src', '../images/edit_on.gif'); }, function () { $('img', this).attr('src', '../images/edit_off.gif'); }); });Thanks for the help.
NinjaCoder8