Mhtml
03-09-2005, 05:08 AM
Beyond form validation my javascript skills are slightly lacking, so I've run into a problem trying to implement a simple collapsable box.
var linkNum = 0;
function add( parent_element, child_element ){
linkNum++;
var parent = document.getElementById(parent_element);
var child = document.getElementById(child_element);
var link = document.createElement("a");
link.id = "collapse_link_"+linkNum;
link.href = "javascript:collapse(this.id,'"+child_element+"')";
link.appendChild(document.createTextNode("-"));
parent.insertBefore(link,child);
}
function collapse( link_id, child_id ){
var child = document.getElementById(child_id);
var link = document.getElementById(link_id);
if( child.style.display == "none" ){
child.style.display = "block";
}else{
child.style.display = "none";
}
}
add( "some_parent", "some_parent_collapsable" );
Given the ID of a parent and child DIV the script first creates an ANCHOR and then handles requests from to collapse or expand the child DIV element. Now the problem I have is, setting the text for the link. But I can't even seem to get a handle to it so I can't change the text.
Reason for dynamically creating the element is so users on older browsers won't even see it. I don't doubt there will be some intersection that loses out, where the browser can expand/collapse but can't write the element so misses out sort of needlessly...but I don't care for them, they miss out. But they won't even know they're missing out.
[edit:] For some reason link_id in collapse() contains link.href instead of link.id
var linkNum = 0;
function add( parent_element, child_element ){
linkNum++;
var parent = document.getElementById(parent_element);
var child = document.getElementById(child_element);
var link = document.createElement("a");
link.id = "collapse_link_"+linkNum;
link.href = "javascript:collapse(this.id,'"+child_element+"')";
link.appendChild(document.createTextNode("-"));
parent.insertBefore(link,child);
}
function collapse( link_id, child_id ){
var child = document.getElementById(child_id);
var link = document.getElementById(link_id);
if( child.style.display == "none" ){
child.style.display = "block";
}else{
child.style.display = "none";
}
}
add( "some_parent", "some_parent_collapsable" );
Given the ID of a parent and child DIV the script first creates an ANCHOR and then handles requests from to collapse or expand the child DIV element. Now the problem I have is, setting the text for the link. But I can't even seem to get a handle to it so I can't change the text.
Reason for dynamically creating the element is so users on older browsers won't even see it. I don't doubt there will be some intersection that loses out, where the browser can expand/collapse but can't write the element so misses out sort of needlessly...but I don't care for them, they miss out. But they won't even know they're missing out.
[edit:] For some reason link_id in collapse() contains link.href instead of link.id