02-04-2010, 10:34 PM
|
#5 (permalink)
|
|
The Addict
Join Date: May 2009
Posts: 287
Thanks: 5
|
This won't allow you to remove them, but it will allow you to only assign the onclick event handlers to specific elements. Just pass it an array of element ID's that you want it to watch and it should work.
javascript Code:
function watch_elms(elms) { // This function will grab the elements to watch and assign a // function to show/hide them when the parent (watched) elmement // is clicked.
var i, count = elms.length, return_elms = [];
for(i = 0; i < count; i++) {
// Assign the elements to an array so we can watch them. return_elms[i] = document.getElementById(elms[i]);
// Now that we're watching them assign an onclick event // handler to them. return_elms[i].onclick = function () {
// Check if the child is visible or not and do the // opposite action (show if hidden and hide if shown). if (return_elms[i].firstChild.style.display === 'none') { // Show the child element // You could use inline, inherit, or auto (CSS properties) return_elms[i].firstChild.style.display = 'block'; } else { // Hide the child return_elms[i].firstChild.style.display = 'none'; }
}
}
return return_elms; }
// We should return the elements to get them on a level where // you can manage them more (if you keep them in a variable // you risk losing them when the function exists and the non- // ability to edit the properties further. var watched_elms = watch_elms(['a_parent', 'b_parent']);
|
|
|
|