...

View Full Version : getElementsByTag name problem?



storky
12-14-2011, 11:22 PM
I have never used this object before and don't know a whole lot about javascript or DOM. I am trying to create a select menu so that when you click an option it turns the display attribute of the coorosponding div to just "" from the initial "none". This works fine just by grabbing the right ID.

I also want to hide all other divs, even if they were previsously selected by mistake, so I thought I could use the get elements by tage name object to grab all the divs and then edit their display to "none". Maybe this is completely the wrong way to go about what I want to do, because eventually there will have to be forms nested within these divs. Any help would be appreciated, with the code or the method. Here is the function I have so far. nursing is the option it should display. I tried calling the length of my array and it came up correctly as 2, idk if that helps.



<script type="text/javascript">
function nursing()
{
y= "nursing"
divs = document.getElementsByTagName("div");

if (document.getElementById(y).style.display="none")
{
document.getElementById(y).style.display="";
}

for (x in divs)
{
if(divs.Id!=y)
{
divs.style.display="none";
}
}
}

Old Pedant
12-15-2011, 12:18 AM
This is wrong:


if(divs.Id!=y)


The property is id, not Id. JavaScript is case sensitive.

Also, the elements collection can't be iterated via the for...in syntax.

Also this line

divs.style.display="none";
is completely wrong. It is attempting to apply the style to the entire collection of <div>s.

So:


for (var x = 0; x < divs.length; ++x )
{
if ( divs[x].id != y ) divs[x].style.display = "none";
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum