Jump to content

Capture Mouse Event.


The Little Guy

Recommended Posts

I would like to capture the mouse event, when the user does something to a link, when the user mouse overs it, and change it when the user mouse's out of it.

 

Why doesn't this work?

function highlight(e,id){
e.onMouseover=function(){
	document.getElementById(id).style.textDecoration = 'underline';
}
e.onMouseout=function(){
	document.getElementById(id).style.textDecoration = 'none';
}
}

Link to comment
Share on other sites

Passing in the event and id are really not necessary for simply triggering a function like that. By the time you set your highlight function, your onmouseover has already been triggered. Try something like this: assign a recognizable class to all the anchor tags which you want this method applied. Then, when your page loads, dynamically apply the event handler to all the different pieces:

 

Javascript:

window.onload = function()
{
  var eles = document.getElementsByTagName('a');
  for (var i = 0; i < eles.length; i++)
  {
    if (eles[i].className == 'hover-me')
    {
      eles[i].onmouseover = function()
      {
        this.style.textDecoration = 'underline';
      }

      eles[i].onmouseout = function()
      {
        this.style.textDecoration = 'none';
      }
    }
  }
}

 

The above code will execute when the page loads to set all links with the class of "hover-me" to react in the way defined by the functions. So, all you have to do is set up your links:

<a href="#" class="hover-me">I will change</a>
<a href="#">I will not</a>

 

Good luck!

Link to comment
Share on other sites

obsidian, that messes up my page's layout some how...

 

Hmm... that's odd. My function doesn't do anything with layout at all... Do you have other JavaScript that needs to run on page load as well? My onload assignment may overwrite something you have in place...

Link to comment
Share on other sites

You are going about this the wrong way and obsidian's answer to attach the event to the object is the way to do it. The only issue is that it may overwrite other onload events.

 

You may be able to use

 

window.attachEvent('onload', function); //where function is the name of your function

Link to comment
Share on other sites

You may be able to use

 

window.attachEvent('onload', function); //where function is the name of your function

 

Good call, emehrkay.

 

If you can indeed use the attachEvent for the handler, that would be great, since it will pile up all the handlers that you attach. So, whatever you run for your footer would need to be attached as well as the code that I sent you. If you have issues with attaching more than one action to the event, you can use my previous window.onload code, but you'll need to place everything that needs to happen at page load within that one function declaration.

Link to comment
Share on other sites

not my favorite way, but It might just have to do...

 

function highlight(evnt,id){
if(evnt == 'over'){
	document.getElementById('message_'+id).style.textDecoration = 'underline';
}else if(evnt == 'out'){
	document.getElementById('message_'+id).style.textDecoration = 'none';
}
}

 

onmouseover="highlight(\'over\',this.id)" onmouseout="highlight(\'out\',this.id)"

Link to comment
Share on other sites

If you are determined to do it inline like that, at least clean it up a bit:

function highlight(ele, action)
{
  if (action == 'over')
  {
    ele.style.textDecoration = 'underline';
  }
  else
  {
    ele.style.textDecoration = 'none';
  }
}

 

<a href="#" onmouseover="highlight(this, 'over');" onmouseout="highlight(this, 'out');">Try me</a>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.