In Javascript, how to get the current id of the div where mouse pointer is?
11,664
Solution 1
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);
}
Solution 2
<div id="the-id" onmouseover="alert(this.id)">some text</div>
Solution 3
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/
Author by
Salman Virk
Updated on June 22, 2022Comments
-
Salman Virk about 2 years
How to get the id of the div on which the mouse is currently pointing?
-
Alberto Zaccagni over 13 years@Downvoters: What's wrong with my answer? Not for the two -1 (don't care) but for the way i solved the problem.
-
lincolnk over 13 yearsno vote from me, but you'd have to do that on every element if you want to determine which one you are currently over.
-
Alberto Zaccagni over 13 yearsOf course, yes, but the op did not specify he wanted something like that. Btw i've understood, thanks. ;)
-
joshua about 10 yearsits simple i can limit it to which divs I want! thanks