Created
May 31, 2012 01:24
-
-
Save ajaswa/2840121 to your computer and use it in GitHub Desktop.
Util.contains
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<script src="util.js"></script> | |
<style> | |
div { | |
border: 3px solid blue; | |
height: 100px; | |
width: 100px; | |
} | |
body > div > div { | |
width: 94px; | |
height: 94px; | |
border-color: black; | |
} | |
body > div > div > div { | |
border-color: green; | |
width: 88px; | |
height: 88px; | |
} | |
body > div > div > div > div { | |
border-color: red; | |
width: 82px; | |
height: 82px; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="el1"> | |
<div id="el2"> | |
<div id="el3"> | |
<div id="el4"> | |
text | |
</div> | |
</div> | |
</div> | |
</div> | |
<button onclick="Util.setup();">Click me first</button> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Util = Util || {}; | |
Util.curry = function (fn, scope) { | |
var scope = scope || window, | |
args = [], | |
i, | |
len; | |
for (i = 2, len = arguments.length; i < len; ++i) { | |
args.push(arguments[i]); | |
} | |
return function () { | |
fn.apply(scope, args); | |
}; | |
}; | |
Util.message = function (message) { | |
alert(message); | |
}; | |
Util.contains = function (parent, child) { | |
// utility to figure out if one element is inside another | |
if (parent === null || child === null) { | |
Util.message('nope'); | |
return false; | |
} | |
if (parent === child.parentNode) { | |
Util.message('yep'); | |
return true; | |
} else { | |
var newChild = child.parentNode, | |
curry = Util.curry(Util.contains, null, parent, newChild); | |
curry(); | |
return; | |
} | |
}; | |
Util.setup = function () { | |
var button, | |
buttonTwo; | |
Util.els = []; | |
Util.els.push(document.getElementById('el1')); | |
Util.els.push(document.getElementById('el2')); | |
Util.els.push(document.getElementById('el3')); | |
Util.els.push(document.getElementById('el4')); | |
button = document.createElement('button'); | |
button.innerHTML = 'Is el4 inside of el2?'; | |
button.addEventListener('click', function () { Util.contains(Util.els[1], Util.els[3]); }, false); | |
document.body.appendChild(button); | |
buttonTwo = document.createElement('button'); | |
buttonTwo.innerHTML = 'Is el1 inside of el3?'; | |
buttonTwo.addEventListener('click', function () { Util.contains(Util.els[2], Util.els[0]); }, false); | |
document.body.appendChild(buttonTwo); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment