Many people have suggested using the jQuery attribute and metadata for this. Here, I simply suggest using a global variable. However, there are a couple of problems you encountered, so let me explain the solutions:
(1). If you put the global variable, beware of href = "" in your anchors. If you start reloading the page, you will return to square zero, but this is not good. So I deleted it.
(2). You want to check the global variable in mouseover and mouseout to do something like
if <triggered from current> return;
Problem
is that in the click function you want to do something like
if <there is a current> current.trigger('mouseout'); <set new current> current.trigger('mouseover')
but since we disabled mouseout and mouseover by disabling validation , the start of these actions does not work.
Here you have two options: a) you can set the current to zero, activate these two events, and then set the current to one click; b) Or you can isolate the effect of overload / shutdown with a trigger so that you can call it in two places.
Option (a) is a more complex click handler, but a simpler handler. Option (b) requires a bit of refactoring, but if you prefer to go the way of using other jQuery functions, this is probably a good idea.
(3). Since I don't use jQuery idioms, but your approach, you need to be careful with the regex so that it matches what it should be. I have done it.
(4). The code below attaches the click to the anchors, as mentioned above (although some solutions mention the images themselves). The problem is how you snap the clicked object to the corresponding image. In the code below, jQuery find () is the key.
(5). In jQuery, it is best to use global variables, i.e. Variables at the window object level. You can still get the same result declaring a variable above the close, as shown below. Also, people like to use $ for variables that contain jQuery wrapped objects, and I did it.
(6). Lastly, beware of comparing objects. The code below allows you to compare DOM objects, so use jQuery get () .
So this is the corrected HTML (remote href):
<a id="anchor1" class="imageLink" ><img class="hoverImage" src="images/1.png"/></a> <a id="anchor2" class="imageLink" ><img class="hoverImage" src="images/2.png"/></a> <a id="anchor3" class="imageLink" ><img class="hoverImage" src="images/3.png"/></a>
(sorry the image urls are not like you, I really tested the code below)
Approach (a):
<script type="text/javascript"> $(function() { var clickedImg = null; // this is common to all closures below $("img.hoverImage") .mouseover(function () { var $img = $(this); if ($img.get(0) == clickedImg) return; // note the improved matching ! var src = $img.attr("src").match(/[^_\.]+/) + "_hover.png"; $img.attr("src", src); }) .mouseout(function () { var $img = $(this); if ($img.get(0) == clickedImg) return; var src = $img.attr("src").replace("_hover",""); $img.attr("src", src); }); $("a.imageLink").click(function() { var oldClicked = clickedImg; clickedImg = null; // set to null to trigger events if (oldClicked) $(oldClicked).mouseout(); var newClicked = $(this).find('img').get(0); $(newClicked).mouseover(); clickedImg = newClicked; // redefine at the end alert($(this).attr('id') + " clicked"); // ajax call here }); }); </script>
and approach (b):
<script type="text/javascript"> $(function() { var clickedImg = null; // same global // refactor the mouse over/out - you could use other jQuery ways here function doOver($img) { var src = $img.attr("src").match(/[^_\.]+/) + "_hover.png"; $img.attr("src", src); } function doOut($img) { var src = $img.attr("src").replace("_hover",""); $img.attr("src", src); } $("img.hoverImage") .mouseover(function () { var $img = $(this); if ($img.get(0) == clickedImg) return; doOver($img); }) .mouseout(function () { var $img = $(this); if ($img.get(0) == clickedImg) return; doOut($img); }); $("a.imageLink").click(function() { if (clickedImg) doOut($(clickedImg)); clickedImg = $(this).find('img').get(0); doOver($(clickedImg)); alert($(this).attr('id') + " clicked"); // presumably your ajax call here }); }); </script>
Option (a) preserves your structure, but I think that option (b) should be your next step, and these are my preferences.