Why not use the parent element's class name instead of id? That way you can have two or more parents with the same class name. For the purpose of the code below, I'm assuming that the parent(s) are division tags with the class name of 'content':
Code:
<script type="text/javascript">
document.onclick=check;
function check(e){
var target = (e && e.target) || (event && event.srcElement);
for (var c = document.getElementsByTagName('div'), i = c.length - 1; i > -1; --i)
if(c[i].className == 'content')
c[i].style.display = '';
if(checkParent(target))
checkParent(target).style.display = 'none';
};
function checkParent(t){
while(t.parentNode){
if(t.className == 'content')
return t;
t=t.parentNode;
};
return false;
};
</script>
However, I'm a little confused by the original code, here:
Code:
checkParent(target)?obj.style.display='none':null;
Which looks like to me, if I've clicked on a child of the parent, it (the parent and the child) disappears. If I've clicked anywhere else, parent and child appear. If I have that right, I think my code is setup to do that as well.
And I don't like the looks of:
Code:
var target = (e && e.target) || (event && event.srcElement);
But if it works, I'm not going to argue about it.
Bookmarks