PDA

View Full Version : Changing style of elements



benforbes
08-20-2005, 06:10 AM
I need to toggle the 'display' style of a certain element, which I have obtained through getElementsByTagName(). I am finding it very difficult compared to if I were using getElementById(). What is the best way to do this?



function Toggle() {
tables=document.getElementsByTagName('table');
tables[0].style.display='none'; //Doesn't work
}

Mongus
08-20-2005, 07:20 AM
Are you sure you're trying to hide the right table? The code you listed should hide the first table on the page.

Are you getting any errors?

coothead
08-20-2005, 07:41 AM
Hi there benforbes,

here is a working example...

<script type="text/javascript">
<!--
function Toggle() {
tables=document.getElementsByTagName('table');
tables[0].style.display='none';
}
//-->
</script>



<table><tr>
<td>table 0</td>
</tr></table>
<table><tr>
<td>table 1</td>
</tr></table>
<a href="#" onclick="Toggle()">toggle</a>

coothead

benforbes
08-20-2005, 08:29 AM
That works now for some reason. :confused: I don't have the exact code I was originally using, I've since replaced it with another technique. I think I may have been accessing the node collection returned by getElementsByTagName() instead of the element array, if that is even possible. Whatever the case, it's fixed now, thanks.