Connecting Tech Pros Worldwide Help | Site Map

Is it possible to use onClick with an Image in Mozilla?

  #1  
Old July 24th, 2005, 03:25 AM
j_liu21@hotmail.com
Guest
 
Posts: n/a
I'm trying to trigger a javascript call when someone clicks on an
image.

<a href="javascript:void(0)" onClick="alert('hi');"><IMG src="foo.gif"
usemap="#someImageMap" border="0"></a>

This works in I.E., but will only work in Mozilla (using 1.0.5) if
there is text and the user must click on the text and not the image to
trigger the alert:

<a href="javascript:void(0)" onClick="alert('hi');"><IMG src="foo.gif"
usemap="#someImageMap" border="0">works if you click on this text</a>

  #2  
Old July 24th, 2005, 03:55 AM
j_liu21@hotmail.com
Guest
 
Posts: n/a

re: Is it possible to use onClick with an Image in Mozilla?


Actually it seems it does work if I remove the usemap feature.

<a href="javascript:void(0)" onClick="alert('hi');"><IMG src="foo.gif"
border="0"></a>

The problem is I need to use the imagemap to determine where they
click, but because the imagemap is reused multiple times, I don't have
the context of which image the user actually clicked.

<a href="javascript:void(0)" onClick="alert('hi');"><IMG src="foo1.gif"
name="foo1" usemap="#someImageMap" border="0"></a>

<a href="javascript:void(0)" onClick="alert('hi');"><IMG src="foo2.gif"
name="foo2" usemap="#someImageMap" border="0"></a>

Is there any way to capture this context in the map itself where I can
retrieve the name?
<map name="someImageMap">
<area shape="rect" coords="0,0,15,16" onClick="inputValue(1,this)"
ALT="Section 1">
<area shape="rect" coords="16,0,33,16" onClick="inputValue(2,this)"
ALT="Section 2">
</map>

  #3  
Old July 24th, 2005, 05:15 AM
ASM
Guest
 
Posts: n/a

re: Is it possible to use onClick with an Image in Mozilla?


j_liu21@hotmail.com wrote:[color=blue]
> I'm trying to trigger a javascript call when someone clicks on an
> image.
>
> <a href="javascript:void(0)" onClick="alert('hi');"><IMG src="foo.gif"
> usemap="#someImageMap" border="0"></a>[/color]

on my idea
you can't have simultanously a click on
- an image (complete aera of image)
- a part of image (aera from map)
How does browser know what he has to do ? how to choice ?

and your sentance :
[color=blue]
> Is there any way to capture this context in the map itself where I can
> retrieve the name?[/color]

has no signification for me. What are you talking about ?
[color=blue]
> <a href="javascript:void(0)" onClick="alert('hi');"><IMG src="foo.gif"
> usemap="#someImageMap" border="0">works if you click on this text</a>[/color]

of corse ! the link extands to the text ...


--
Stephane Moriaux et son [moins] vieux Mac
  #4  
Old July 24th, 2005, 07:15 AM
RobG
Guest
 
Posts: n/a

re: Is it possible to use onClick with an Image in Mozilla?


j_liu21@hotmail.com wrote:[color=blue]
> Actually it seems it does work if I remove the usemap feature.
>
> <a href="javascript:void(0)" onClick="alert('hi');"><IMG src="foo.gif"
> border="0"></a>
>
> The problem is I need to use the imagemap to determine where they
> click, but because the imagemap is reused multiple times, I don't have
> the context of which image the user actually clicked.
>
> <a href="javascript:void(0)" onClick="alert('hi');"><IMG src="foo1.gif"
> name="foo1" usemap="#someImageMap" border="0"></a>
>
> <a href="javascript:void(0)" onClick="alert('hi');"><IMG src="foo2.gif"
> name="foo2" usemap="#someImageMap" border="0"></a>
>
> Is there any way to capture this context in the map itself where I can
> retrieve the name?
> <map name="someImageMap">
> <area shape="rect" coords="0,0,15,16" onClick="inputValue(1,this)"
> ALT="Section 1">
> <area shape="rect" coords="16,0,33,16" onClick="inputValue(2,this)"
> ALT="Section 2">
> </map>
>[/color]

Pass 'this' from your area elements, then from the inputValue function
call a 'getMap' function that climbs up the node tree looking for
the parent map element, something like:

HTML:

<map ...>
<area ... onClick="inputValue(1, this)" ... >
<area ... onClick="inputValue2, this)" ... >
...
</map>


Script:

function inputValue( num, el ){
var mapRef;
if ( el && (mapRef = getMap(el)) ) {
// do stuff with the map reference
// ...
} else {
// couldn't find the map element
}
}

function getMap( a ){
while ( a.parentNode && 'map' != a.nodeName.toLowerCase() ){
a = a.parentNode;
}
return ( 'map' == a.nodeName.toLowerCase() )? a : false;
}



--
Rob
  #5  
Old July 24th, 2005, 08:05 PM
j_liu21@hotmail.com
Guest
 
Posts: n/a

re: Is it possible to use onClick with an Image in Mozilla?


Hmm, after some investigation into nodes and I'm still missing
something. Maybe a better explanation is to consider the NetFlix stars
example. Employ one common imagemap (star rating), but with the need
to update different fields corresponding to repeated images based on
the common imagemap. How can one take advantage of the imagemap areas
while maintaining the context of the actual image clicked? In other
words, depending upon where javascript is initiated, I can determine
either their rating or the appropriate movie, but not both.

  #6  
Old July 24th, 2005, 09:05 PM
j_liu21@hotmail.com
Guest
 
Posts: n/a

re: Is it possible to use onClick with an Image in Mozilla?


Well, after some more thinking and searching, looks like the Node idea
don't quite work in this situation because there is no hierarchy
between the imagemap and the images that use it. I found this post
back in 1999 without any new posts disputing it. Multiple imagemaps it
is.

http://groups-beta.google.com/group/...3c164d1fff8660

  #7  
Old July 25th, 2005, 01:55 AM
RobG
Guest
 
Posts: n/a

re: Is it possible to use onClick with an Image in Mozilla?


j_liu21@hotmail.com wrote:[color=blue]
> Well, after some more thinking and searching, looks like the Node idea
> don't quite work in this situation because there is no hierarchy
> between the imagemap and the images that use it. I found this post
> back in 1999 without any new posts disputing it. Multiple imagemaps it
> is.
>
> http://groups-beta.google.com/group/...3c164d1fff8660
>[/color]

Ah yes, I thought you wanted to find the map, not the image.

It seems that the area element traps the click event and stops it
propagating.

As Martin suggests in your link, you could implement this in pure
JavaScript or a combination of map element and JavaScript, but there are
accessibility issues in that.


--
Rob
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to trigger event by script Ralph answers 13 November 24th, 2006 12:05 AM
video in javascript chrisdude911 answers 8 March 28th, 2006 09:25 AM
Opening image in new window without padding D. Alvarado answers 14 July 23rd, 2005 10:57 PM
open source javascript project - trouble with document.getElementById lawrence answers 1 July 23rd, 2005 12:30 PM