Salman Virk
Salman Virk

Reputation: 12307

In Javascript, how to get the current id of the div where mouse pointer is?

How to get the id of the div on which the mouse is currently pointing?

Upvotes: 2

Views: 13053

Answers (3)

Fosco
Fosco

Reputation: 38516

You can use a javascript variable to store the current hovered div.. jQuery (or standard JS) could be used to set the event handler to populate the variable.

Visible test at: http://jsfiddle.net/gfosco/Hys7r/

Upvotes: 2

lincolnk
lincolnk

Reputation: 11238

I think your best bet is to track mouseover at the document level and maintain the id of the last element hit.

var lastID = null;

var handleMouseover = function (e) {
    var target = e.target || e.srcElement;
    lastID = target.id;
};

if (document.addEventListener) {
    document.addEventListener('mouseover', handleMouseover, false);
}
else {
    document.attachEvent('onmouseover', handleMouseover);
}

Upvotes: 8

Alberto Zaccagni
Alberto Zaccagni

Reputation: 31560

<div id="the-id" onmouseover="alert(this.id)">some text</div>

Upvotes: 2

Related Questions