views:

196

answers:

1

Let's say you have two images that use the same image map.

<img src="/test/image1.jpg" id="image1" useMap="map-name">
<img src="/test/image2.jpg" id="image2" useMap="map-name">

<map name="map-name">
  <area shape="rect" coords="0,0,82,126" alt="Sun" onmouseover="testFunction();"/>
  <area shape="circle" coords="90,58,3"  alt="Mercury" onmouseover="testFunction();"/>
  <area shape="circle" coords="124,58,8" alt="Venus" onmouseover="testFunction();"/>
</map>

Is there any way to get the ID of the image (image1|image2 in this case) inside of the testFunction()?

A: 

Yes. The easiest way is to use a library like jQuery to hook up the event, instead of using onmouseover. If you do that, you'll basically do something like:

$("AREA").mouseOver(function (e) {
  var id = $(e.target).attr("id");
}

If you don't do that then ... well play around with "this" inside your definition of testFunction, and good luck (because you'll get to deal with the joy of cross-browser incompatibility).

machineghost
-1 did you try this solution? #1 There is no `getTarget()` neither in jQuery nor on the DOM Event object itself.
jitter
There is no getTarget() but there is the e.target, e.currentTarget and e.relatedTarget properties in the event object. Is that what you are referring to? If yes, those properties give erratic results - some times it returns the ID of the <area> and other times it returns the ID of the image.Anybody got any other suggests or can you clarify what you were going for machineghost? Thanks for your original answer.
Whoops; don't know where I got getTarget() from ... *sigh* ... but yes, e.target and e.currentTarget is what I had meant. I'll fix the answer. As for the inconsistency, that seems strange. Can you identify any pattern to when it returns the image map's ID vs. the area's?
machineghost
One thought which *might* help resolve the inconsistency is to try using slightly different events (mouseenter or mouseleave), as demonstrated here: http://stackoverflow.com/questions/1201052/how-do-i-highlight-parts-of-an-imagemap-on-mouseover
machineghost