0

Edit: I need to mention that I do not want to use jQuery.
This is my code. I need to access the element which triggered the event such that I don't have to make two different functions for each html element.

document.getElementById("login").onmouseover = turnWhite;

function turnWhite(e){  
}

I need maybe something like this. Don't know if it's possible though.

function turnWhite(e){
    e.HTMLEL.style.color = "white"; 
}

Is this possible?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Vlad Otrocol
  • 2,952
  • 7
  • 33
  • 55
  • possible duplicate of [Getting the ID of the element that fired an event using jQuery](http://stackoverflow.com/questions/48239/getting-the-id-of-the-element-that-fired-an-event-using-jquery) – Marc B Mar 08 '13 at 21:10
  • He's not using jQuery. – Barmar Mar 08 '13 at 21:12

5 Answers5

4

According to javascripter.net

  • e.srcElement in Internet Explorer
  • e.target in most other browsers.
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

If you wanted to apply the same event function to a set of elements, you could try something like this:

var buttons = document.getElementsByClassName("change");
for(var i = 0;i < buttons.length;i++){
    buttons[i].onmouseover = function(){
        this.style.color = "red"; 
    }
}

Demo: http://jsfiddle.net/louisbros/rt24U/

louisbros
  • 865
  • 5
  • 10
0

You should be able to use e.target or e.currentTarget. However, behavior seems to be different per browser... Best to use a library like jquery or yui. Here is some documentation.

Lucas
  • 14,227
  • 9
  • 74
  • 124
0

Here is the JSFiddle so you can play with it: http://jsfiddle.net/9nr4Y/

HTML:

<div class="login">Login</div>
<br />
<div class="login">Login 2</div>

JavaScript:

(function() {
    var elms = document.getElementsByClassName("login"),
        l = elms.length, i;
    for( i = 0; i < l; i++ ) {
        ( function( i ) {
            elms[i].onmouseover = function() {
                this.style.color = "#000000";
                this.style.background = "#FFFFFF";
            };
        })(i);

        ( function( i ) {
            elms[i].onmouseout = function() {
                this.style.color = "#FFFFFF";
                this.style.background = "#000000";
            }
        })(i);
    }
})();

Create a self-invoking function that then gets the elements by a class name. The reason for this is because you don't really want to have more than one element with the ID anyway. Getting the class name will generate an Array of items that have that class. Then you can iterate over the new array and assign the event to each one of them.

Then we're getting each individual element with "this" instead of the actual event.

For reference: How to get current element in getElementsByClassName

Community
  • 1
  • 1
0

You could also go with this non-standard cross-browser hack:

document.getElementById("login").onmouseover = function () { turnWhite(); }

function turnWhite(){  
    var myEventElement = turnWhite.caller.arguments[0].target;
    //do something with myEventElement
}

Useful if you want to avoid passing parrams.

Demo: http://codepen.io/mrmoje/pen/avbEm

mrmoje
  • 3,714
  • 3
  • 20
  • 18