-
Notifications
You must be signed in to change notification settings - Fork 1
/
targeter.js
47 lines (40 loc) · 1.29 KB
/
targeter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
function handler(e) {
// Reset event handler
document.body.style.cursor = "default";
document.removeEventListener('click', handler, false);
// Get selected element
e = e || window.event;
let target = e.target || e.srcElement;
// Find the nearest element with `id` or `name` attribute
target = ((target) => {
// Check selected element
if (target == document.body) {
return null;
}
if (target.id) {
return target.id;
}
if (target.name) {
return target.name;
}
// Recursively check an element's children
function findChildTarget(node, step) {
if (node.id || node.name) {
return node.id || node.name;
}
}
// Recursively check current element's children, starting from the front
})(target);
// Do something with that element
console.log("I think the ideal target would be " + target.id);
while (!target.id) {
console.log(target);
if (target == document.body) {
return;
}
target = target.previousElementSibling || target.parentNode;
}
console.log(target);
}
document.addEventListener('click', handler, false);
document.body.style.cursor = "crosshair";