adam wrote:
getElementsByTagName reports itself as not being a function in firefox
1.0. is this an IE only DOM thing? getElementsByName reports no other
error than the addressed object having no properties.
getElementsByTagName is a method of a DOM element object. It works
(AFAIK)[1] in any browser that supports getElementById. You need to
use it with an element, e.g.
// Using the 'document' object
var allTheInputs = document.getElementsByTagName('INPUT');
// Using an element
var divRef = document.getElementById('someDiv');
var allTheInputsInTheDiv = divRef.getElementsByTagName('INPUT');
thanks for the input though. there's definitely something screwy goin
on.
here's a rough version of the messy, test-riddled code i've got:
Pleas don't post code with tabs, replace with 2 spaces for indents.
function showChildren(chkBoxName,divName) {
if((boxName.checked) == (true)) {
'boxName' is not defined, I guess you meant 'chkBoxName'. It seems to
be a reference to a checkbox, so the test can be:
if( chkBoxName.checked ) {
...
If it's checked, the above will return true.
document.getElementById(divName).style.visibility = "visible";
document.getElementById(divName).style.height = "100%";
document.getElementById(divName).style.width = "100%";
} else {
document.getElementById(divName).style.visibility = "hidden";
document.getElementById(divName).style.height = "0";
document.getElementById(divName).style.width = "0";
All this seems to be hiding/showing an element with the same behaviour
as the display property. You can replace all of the above with:
var parentNode = document.getElementById(divName);
if ( chkBoxName.checked ) {
div.style.display = '';
} else {
div.style.display = 'none';
It could be trimmed down even further if the following is not
necessary.
var parentNode = document.getElementById(divName);
Already done this, so delete it.
var allMyChildren= parentNode.childNodes;
childNodes returns an HTML collection, so 'allMyChildren' is a
collection.
var divChildren =
allMyChildren.getElementsByTagName("DIV");
Collections (like arrays) do not have the getElementsByTagName method,
so 'divChildren' will be undefined - your script likely goes no
further, certainly anything dependending upon divChildren to have a
value will not work.
divChildren.style.visibility = "hidden"; // doesn't work
whatsoever, returns no error
var inputChildren =
parentNode.childNodes.getElementsByTagName("INPUT" );
parentNode.childNodes is a collection, so no getElementsByTagName
method...
alert(inputChildren.length); // this test returns
correctly
Dependind on what 'correctly' means to you :-)
If you hide the parent, all the decendents will be hidden too, there is
no need to hide them individually - unless you want them to be
displayed one by one when clicked on.
for (var i=0;i < allMyChildren.length;i++) { //loops proper number of
times but for the code within breaking it.
var test = document.getElementById(divName).item[i];
If you are after the first child of the element (which seems to be
'parentNode' again), then you can find using:
var test = parentNode.firstChild;
if (test.type == "checkbox") {
test.checked = chk.unchecked;
if ( test.type && 'checkbox' == test.type ) {
test.checked = false;
[...]
When I read your first post, I thought you wanted to uncheck checkboxes
that were decendants of other checkboxes, however the above code seems
to be also hiding and showing elements. Below is some code that just
does the checkbox thing - it also checks all the parents of a
checkbox so you can just check some lower class and all the parents are
done too.
[1] Completely unsupported opinion, but I'd be very surprised if any
browser in common use that supported getElementById did not also
support getElementsByTagName.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html><head><title> Trees </title>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1">
<style type="text/css">
..treeTrunk, .treeBranch {font-family: arial, verdana, sans-serif;}
..treeTrunk {
color: #3333ee; background-color: #DDDDDD; font-weight: bold;
}
..treeBranch {
border: 1px solid red; font-weight: normal; margin-left: 2em;
}
</style>
<script type="text/javascript">
function doChecks(x) {
// if this one is unchecked, uncheck all decendents
if ( ! x.checked ) {
var cbs = x.parentNode.getElementsByTagName('INPUT');
for (var i=0, len=cbs.length; i<len; i++){
cbs[i].checked = false;
}
}
// if this one is checked, check all ancestors
if ( x.checked ) {
while ( ( x = x.parentNode ) ) {
if ( 'DIV' == x.nodeName && x.firstChild ) {
if ( 'checkbox' == x.firstChild.type ) {
x.firstChild.checked = true;
}
}
}
}
}
function initTree(t){
if (document.getElementById && document.getElementsByTagName ) {
t = document.getElementById(t);
var cbs = t.getElementsByTagName('INPUT');
var i, x, j = cbs.length;
for ( i=0; i<j; i++ ) {
x = cbs[i];
if ( 'checkbox' == x.type ) {
x.onclick = function () {doChecks(this);};
}
}
}
}
</script>
</head><body onload="initTree('tree');">
<div id="tree">
<div class="treeTrunk"><input type="checkbox">
Main branch 1<br>
<div class="treeBranch"><input type="checkbox">
Sub branch 1 1<br>
<div class="treeBranch"><input type="checkbox">
Sub sub branch 1 1 1
</div>
</div>
</div>
<div class="treeTrunk"><input type="checkbox">
Main branch 2<br>
<div class="treeBranch"><input type="checkbox">
Sub branch 2 1<br>
<div class="treeBranch"><input type="checkbox">
Sub sub branch 2 1 1
</div>
<div class="treeBranch"><input type="checkbox">
Sub sub branch 2 1 2
</div>
</div>
<div class="treeBranch"><input type="checkbox">
Sub branch 2 2<br>
<div class="treeBranch"><input type="checkbox">
Sub sub branch 2 2 1
</div>
<div class="treeBranch"><input type="checkbox">
Sub sub branch 2 2 2
</div>
</div>
</div>
</div>
</body>
</html>
--
Rob