Connecting Tech Pros Worldwide Help | Site Map

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

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 24th, 2005, 02:25 AM
j_liu21@hotmail.com
Guest
 
Posts: n/a
Default Is it possible to use onClick with an Image in Mozilla?

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, 02:55 AM
j_liu21@hotmail.com
Guest
 
Posts: n/a
Default 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, 04:15 AM
ASM
Guest
 
Posts: n/a
Default 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, 06:15 AM
RobG
Guest
 
Posts: n/a
Default 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, 07:05 PM
j_liu21@hotmail.com
Guest
 
Posts: n/a
Default 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, 08:05 PM
j_liu21@hotmail.com
Guest
 
Posts: n/a
Default 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, 12:55 AM
RobG
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.