barkermn01
10-28-2010, 03:41 PM
Hello all this has come in so useful in our office after i wrote it years ago so i though i would post it on here for every one to use
WHAT IT DOES
this script allows you to set the link of a parent div,li,table-cell to that of a child <a> tag
HOW TO USE
This system is so simple to use all you have to do is on the tag you would like the parent to act as if it were the link just add the class parentLinkActive to it this dose not prevent you from using your own classes it will search the class for the parentLinkActive and not inter-fear with the other classes. then just run the function i use it onLoad of the body but if using with jQuery stick it in the document ready
E.G
<div id="parent"><a class="parentLinkActive" href="javascript:alert('hello');">Say Hello</a></div>
This now when ever any one click on the div parent it will act like the a link
SOURCE CODE
<script type="text/javascript">
/*
*/
var ParentClickLinks = [];
function childLinksCheck() {
var elements = document.getElementsByTagName('a');
for (var i = 0; i < elements.length; i++) {
var currEl = elements[i];
var pos = currEl.className.indexOf('parentLinkActive');
if (pos != -1) {
var parent = currEl.parentNode;
ParentClickLinks[ParentClickLinks.length] = currEl.href;
parent.className = parent.className + " click_"+(ParentClickLinks.length - 1);
parent.addEventListener('click', function () {
var class = this.className;
var parts = class.split(' ');
for(var c=0; c<parts.length; c++){
if(parts[c].indexOf('click_') != -1){
var indexs = parts[c].split('_');
var pos = indexs[1];
document.location = ParentClickLinks[pos];
}
}
}, false);
currEl.href = "javascript:return false;";
parent.style.cursor = currEl.style.cursor;
}
}
}
/* add "childLinksCheck();" to onload of body */
</script>
WHAT IT DOES
this script allows you to set the link of a parent div,li,table-cell to that of a child <a> tag
HOW TO USE
This system is so simple to use all you have to do is on the tag you would like the parent to act as if it were the link just add the class parentLinkActive to it this dose not prevent you from using your own classes it will search the class for the parentLinkActive and not inter-fear with the other classes. then just run the function i use it onLoad of the body but if using with jQuery stick it in the document ready
E.G
<div id="parent"><a class="parentLinkActive" href="javascript:alert('hello');">Say Hello</a></div>
This now when ever any one click on the div parent it will act like the a link
SOURCE CODE
<script type="text/javascript">
/*
*/
var ParentClickLinks = [];
function childLinksCheck() {
var elements = document.getElementsByTagName('a');
for (var i = 0; i < elements.length; i++) {
var currEl = elements[i];
var pos = currEl.className.indexOf('parentLinkActive');
if (pos != -1) {
var parent = currEl.parentNode;
ParentClickLinks[ParentClickLinks.length] = currEl.href;
parent.className = parent.className + " click_"+(ParentClickLinks.length - 1);
parent.addEventListener('click', function () {
var class = this.className;
var parts = class.split(' ');
for(var c=0; c<parts.length; c++){
if(parts[c].indexOf('click_') != -1){
var indexs = parts[c].split('_');
var pos = indexs[1];
document.location = ParentClickLinks[pos];
}
}
}, false);
currEl.href = "javascript:return false;";
parent.style.cursor = currEl.style.cursor;
}
}
}
/* add "childLinksCheck();" to onload of body */
</script>