Add onmouseover function to all hyperlinks

Hi,

I am having great difficulty adding onmouseover events programatically to all links.
I simply want to have all links display their title attribute in the window.status(bar) when you mouseover.

Please see the attached code - the basic framework for the code is their, I JUST CANT ADD THE ONMOUSOVER EVENT.
//----------------------------------------------------------
// Attaches a function to the event of specified object
// @param obj the object the event is being attached to
// @param eventType the event type (e.g. "load") - do not include "on" as in "onload"!
// @param fn the function to attach
//----------------------------------------------------------
function addEvent(obj, eventType, fn)
{
	if (obj.addEventListener)
	{
		obj.addEventListener(eventType, fn, true);
		return true;
	}
	else if (obj.attachEvent)
	{
		var r = obj.attachEvent("on" + eventType, fn);
		return r;
	}
	else
	{
		return false;
	}
}
//--------------------------------------------------------------------
// Make status bar display all titles on links.
//--------------------------------------------------------------------
addEvent(window, "load", function(){
 
    for(var i=0;i<document.links.length;i++)
    {
        if (document.links[i].title && document.links[i].title != null && document.links[i].title != "")
        {
            addEvent(document.links[i], "mouseover", function(){ 
                alert(this.title); //THIS IS NOT WORKING - WHY??? IT SHOULD ALERT THE LINK TITLE ATTRIBUTE.
                //window.status=this.title; 
                //return true; 
           });
        }   
    }
});

Open in new window

smaccaAsked:
Who is Participating?
 
hieloConnect With a Mentor Commented:
Here you go:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript"><!--
//----------------------------------------------------------
// Attaches a function to the event of specified object
// @param obj the object the event is being attached to
// @param eventType the event type (e.g. "load") - do not include "on" as in "onload"!
// @param fn the function to attach
//----------------------------------------------------------
function addEvent(obj, eventType, fn)
{
	if (obj.addEventListener)
	{
		obj.addEventListener(eventType, fn, true);
		return true;
	}
	else if (obj.attachEvent)
	{
		var r = obj.attachEvent("on" + eventType, fn);
		return r;
	}
	else
	{
		return false;
	}
}
//--------------------------------------------------------------------
// Make status bar display all titles on links.
//--------------------------------------------------------------------
addEvent(window, "load", function(){
 
    for(var i=0,limit=document.links.length ;i<limit;i++)
    {
        if (document.links[i].title && document.links[i].title != null && document.links[i].title != "")
        {  var t= document.links[i].title;
            addEvent(document.links[i], "mouseover", setStat(t) );
        }   
    }
});
 
function setStat(s)
{
	return function(){window.status=s; return true;}
}
//--></script>
</head>
<body>
<ul>
	<li><a href="#" title="alpha">Alpha</a></li>
	<li><a href="#" title="beta">Beta</a></li>
</ul>
</body>
</html>

Open in new window

0
 
Computer101Commented:
Forced accept.

Computer101
EE Admin
0
All Courses

From novice to tech pro — start learning today.