So I have an array of strings (I think I did it right):
var editableObjects = ['.body-copy', 'img', '.subhead', '.meta', '.caption', '.hyperlink', '.bullet'];
A function that gets the element that is under the mouse:
function getMouseElement(event) {
var posX = event.clientX,
posY = event.clientY;
// get the element that is located under the mouse
var overElem = document.elementFromPoint(posX, posY);
return overElem;
}
And then this jquery listener that watches for mouseover on ".editable" elements:
$('#wrapper').on('mouseover', '.editable', function(event) {
var m = getMouseElement(event);
console.log(m.className); /*This correctly returns the tag of the element under the mouse*/
for (var c = editableObjects.length - 1; c >= 0; c--) {
var o = editableObjects[c];
if (m.className = o) {
console.log(m + ' matches ' + o);
} else {}
}
});
What I'm trying to do, is have the function look through the list and see if the element the mouse is over matches one of the string objects in editableObjects. My for-loop is supposed to check the variable for any of those matching elements, and then do something if there is a match. But after many hours, I can only get the for loop to return a match for every object in the variable, whether it actually matches or not.
mouseoverevent is fired over certain elements why not simply put the event listener on those elements rather than the parent and then figuring cursor position? i.e.,$('.body-copy').on('mouseover',... $('.subhead').on('mouseover'...etc.