Reputation: 365
I am trying to get the element that the mouse is currently over and return it. From what I've gathered thus far from research, this is the code to do it:
document.getElementById('theTable').onmouseover = function () {
var x = event.ClientX, y = event.ClientY,
elementMouseIsOver = document.elementFromPoint(x, y);
alert(elementMouseIsOver);
}
However, all the elements I created have ID's but I always get a constant return of "[object HTMLDivElement]" I'm expecting it to tell me what the ID of that element is...am I misusing the code? This will become a navigation bar on a web part within SharePoint.
Thanks in advance for any helpful input.
All Code:
var siteUrl = '/sites/dev/';
var theCounter = 0;
ExecuteOrDelayUntilScriptLoaded(retrieveListItems, "sp.js");
function retrieveListItems() {
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('myList');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml("<Where><And><IsNotNull><FieldRef Name='Title' /></IsNotNull> <IsNotNull><FieldRef Name='TitleLink' /></IsNotNull></And></Where>");
this.collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded(sender, args)
{
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext())
{
var oListItem = listItemEnumerator.get_current();
//Each column in in the SharePoint List will essentially become an array.
//So make an array for each column that will be returned!
var theHeaders = new Array();
var HeaderLinks = new Array();
theCounter += 1;
theHeaders[theCounter - 1] = oListItem.get_item('Title');
HeaderLinks[theCounter - 1] = oListItem.get_item('TitleLink');
//Get the Table Element created in HTML
var getTheTableTag = document.getElementById('theTable');
//Create the headers (top level links)
var createTheHeaderElements = document.createElement('td');
createTheHeaderElements.id = 'headerTag';
var link = document.createElement('a');
link.id = 'headerLinksTag';
var theHeaderText = document.createTextNode(theHeaders[theCounter - 1]);
link.setAttribute("href", HeaderLinks[theCounter - 1]);
link.appendChild(theHeaderText);
createTheHeaderElements.appendChild(link);
getTheTableTag.appendChild(createTheHeaderElements);
// var createA = document.createElement('a');
// var createAText = document.createTextNode(theCounter);
// createA.setAttribute('href', "http://google.com");
// createA.appendChild(createAText);
// getTheTableTag.appendChild(createA);
};
////////////////////////////HERE IS THE PROBLEM CHILD//////////////////////
document.getElementById('theTable').onmouseover = function () {
var x = event.ClientX, y = event.ClientY,
elementMouseIsOver = document.elementFromPoint(x, y);
alert(elementMouseIsOver);
}
///////////////////////////////////////////////////////////////////////////
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
Upvotes: 1
Views: 3960
Reputation: 59
document.getElementById('theTable').onmouseover = function (e) {
console.log(e.target)
alert("mouse is over " + e.target.id);
}
Upvotes: 1
Reputation: 4670
Two things come to mind.
Try passing function(event)
instead of simply function()
for the onmouseover
function.
event.target.id
usually gets the id - try that.
Upvotes: 3