/    Sign up×
Community /Pin to ProfileBookmark

Hiding every other sub-element

Hi, I’m having some trouble with this code, I’m not quite sure what’s wrong, but when I click the item in the combo box, nothing happens. I’ve managed to get the elements to show and hide if I specify them explicitly by name, but I have quite a few of them, so doing it explicitly would take long and be impractical.

Here’s the Javascript function I made:

[code=php]
function HideShow(what)
{
var parent = document.getElementById(“parent”);

for (i=0; i < parent.elements.length; i++)
{
current = parent.elements[i];
if (current.id == what)
{
current.style.visibility = “visible”;
}
else
{
current.style.visibility = “hidden”;
}
}
[/code]

The combobox from which it is called:

[code=html]
<select name=”showhide” id=”showhide”>
<option value=”1″ onclick=”HideShow(‘sub1’)”>Show 1</option>
<option value=”2″ onclick=”HideShow(‘sub2’)”>Show 2</option>
<option value=”3″ onclick=”HideShow(‘sub3’)”>Show 3</option>
</select>
[/code]

And the tags I’d want shown and hidden would be structured like this:

[code=html]
<div id=”parent”>
<div id=”sub1″ style=”visibility:hidden”>
Content.
</div>
<div id=”sub2″ style=”visibility:hidden”>
Content.
</div>
<div id=”sub3″ style=”visibility:hidden”>
Content.
</div>
</div>
[/code]

In case you didn’t understand, I want it so when that function is called, just the DIV with the ID specified in the ‘what’ parameter to be shown, and the rest to be hidden.

Thanks to anyone who can help me debug this, for some reason document.write doesn’t work in the middle of the loop, so I can’t tell what’s wrong ?.

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@aprilaprilAug 16.2007 — [CODE]
function HideShow(what)

{
var parent = document.getElementById("parent");
var subDivs = parent.getElementsByTagName("div");

for (i=0; i < subDivs.length; i++)
{
current = subDivs[i];
if (current.id == what)
{
current.style.visibility = "visible";
}
else
{
current.style.visibility = "hidden";
}
}
}

[/CODE]
Copy linkTweet thisAlerts:
@CodeMaster2authorAug 16.2007 — Still doesn't work ?. Any other ideas?
Copy linkTweet thisAlerts:
@aprilaprilAug 16.2007 — work on FF, not on IE.

change the drop down element to:

<select name="showhide" id="showhide" onChange="HideShow(value)">

<option value="sub1">Show 1</option>

<option value="sub2">Show 2</option>

<option value="sub3">Show 3</option>

</select>
Copy linkTweet thisAlerts:
@CodeMaster2authorAug 16.2007 — I'm using Opera and FF, target browser is Opera really, but doesn't matter. That snippet doesn't work either. On either browsers.
Copy linkTweet thisAlerts:
@aprilaprilAug 16.2007 — I think there must be something wrong with ur browsers. I have test in FF and IE, all works correctly.
Copy linkTweet thisAlerts:
@CodeMaster2authorAug 16.2007 — Umm, I don't think there's anything wrong with my browsers, I even tested on two different computers (Opera, FF and IE on all of them).

Here's the test page I was using, perhaps you could post the code that you're testing with?

[code=html]<html>
<head>
<title>Test</title>
<script type="text/javascript">
function HideShow(what)

{
var parent = document.getElementById("parent");

for (i=0; i < parent.elements.length; i++)
{
current = parent.elements[i];
if (current.id == what)
{
current.style.visibility = "visible";
}
else
{
current.style.visibility = "hidden";
}
}
</script>
</head>

<body>
<p><select name="showhide" id="showhide" onChange="HideShow(value)">
<option value="sub1">Show 1</option>
<option value="sub2">Show 2</option>
<option value="sub3">Show 3</option>
</select></p>

<div id="parent">
<div id="sub1" style="visibility:hidden">1</div>
<div id="sub2" style="visibility:hidden">2</div>
<div id="sub3" style="visibility:hidden">3</div>
</div>

</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@aprilaprilAug 16.2007 — [CODE]
<html>
<head>
<title>Test</title>
<script type="text/javascript">
function HideShow(what) {
var parent = document.getElementById("parent");
var subDivs = parent.getElementsByTagName("div");
for (i=0; i < subDivs.length; i++) {
current = subDivs[i];
if (current.id == what) {
current.style.visibility = "visible";
} else {
current.style.visibility = "hidden";
}
}
}
</script>
</head>
<body>
<p>
<select name="showhide" id="showhide" onChange="HideShow(value)">
<option value="sub1">Show 1</option>
<option value="sub2">Show 2</option>
<option value="sub3">Show 3</option>
</select>
</p>
<div id="parent">
<div id="sub1" style="visibility:hidden">1</div>
<div id="sub2" style="visibility:hidden">2</div>
<div id="sub3" style="visibility:hidden">3</div>
</div>
</body>
</html>
[/CODE]
Copy linkTweet thisAlerts:
@CodeMaster2authorAug 16.2007 — Yay, it worked! Thanks man.
×

Success!

Help @CodeMaster2 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...