[thelist] JavaScript - adding onclick events to links that do not execute (advanced)

Peter-Paul Koch pp.koch at gmail.com
Fri Nov 25 11:09:40 CST 2005


> I'm adding some onclick events to anchors/links using JavaScript during the onload. The onclick
> should fire a function then return false so the href doesn't get followed. However, the return
> false does not seem to fire/work in my anonymous function. Here is the code:

I copied the code, added an empty ajaxController() function, and it
worked fine: the links were not followed. I think the bug is in
ajaxController().

>
> =======================================================
> function addEventsToLinks()
> {
>         var filmstripItems = new Array();
>         filmstripItems[0] = "colorado";
>         filmstripItems[1] = "scene2";
>         filmstripItems[2] = "scene3";
>
>         var filmstrip = document.getElementById('filmstrip');
>         var anchors = filmstrip.getElementsByTagName('a');
>
>         for(var y=0; y<anchors.length; y++)
>         {
>                 anchors[y].onclick = function() {
>                         ajaxController("load_full", ""+ filmstripItems[y] +"");
>                         return false;
>                 }
>                 anchors[y].href = "http://www.google.com/";
>         }
> }
> window.onload = addEventsToLinks;
>
>
> <div id="filmstrip">
> <a href=""><img src="colorado_thumb.jpg" width="100" height="75" alt="" border="0" /></a>
> <a href=""><img src="redrock1_thumb.jpg" width="100" height="75" alt="" border="0" /></a>
> <a href=""><img src="redrock2_thumb.jpg" width="100" height="75" alt="" border="0" /></a>
> </div>
> =======================================================

--
-------------------------------------------------------------------
ppk, freelance web developer
http://www.quirksmode.org/
------------------------------------------------------------------



More information about the thelist mailing list