/    Sign up×
Community /Pin to ProfileBookmark

Displaying just portions of a list

I have an unordered list of content with unique ID for each item. I also have a menu of buttons that should display a given list item. I want to hide all items in the list at first, but then use the buttons to display their appropriate list item. If a given item in the list is already displayed when another button is pressed, the currently shown item should disappear and be replaced with the new one. My HTML is as follows:

[code=html]<ul id=”my_list” >
<li id=”item1″>stuff</li>
<li id=”item1″>stuff</li>
<li id=”item1″>stuff</li>
</ul>

<ul id=”menu”>
<li><input type=”button” value=”Item 1″ onclick=”showItem(‘item1’);” /></li>
<li><input type=”button” value=”Item 2″ onclick=”showItem(‘item2’);” /></li>
<li><input type=”button” value=”Item 3″ onclick=”showItem(‘item3’);” /></li>
</ul>[/code]

To originally hide all items, I am under the impression that I can not just target the ul tag, but must target the individuals li tags since display is not inherited. Does this sound correct?

[CODE]#my_list li {display: none;}[/CODE]

To hide any previous item, and show the new item, I was thinking of the following:

[CODE]function showItem(currMenu) {
if (document.getElementById) {
thisMenu = document.getElementById(currMenu);
thisMenu.parentNode.style.display = “none”;
thisMenu.style.display = “block”;
return false;
}
else {
return true;
}
}[/CODE]

The list items are initially hidden, but unfortunately, the javascript does not make the selected item appear. Any suggestions what I am doing wrong?

Thanks

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@mrhooJan 22.2007 — thisMenu.parentNode.style.display = "none";[/QUOTE]
Lists- ul or dl or ol , when set to 'display:none', cannot display any child nodes. Leave the ul display alone, and change the display of the list items.

When you have a reference to a list item to display, go through its nextSiblings and previousSiblings, and set their display to none.

Or invest in a global to keep track of the most recent visible item in any list.
Copy linkTweet thisAlerts:
@NotionCommotionauthorJan 22.2007 — Thanks mrhoo,

What do you think of the following? To make it work, I had to put a hidden element in my HTML, and then during my onload script, set thisMenu equal to the temporary element.
[CODE]function hideDisplay(currMenu) {
thisMenu.style.display = "none";
thisMenu = document.getElementById(currMenu);
thisMenu.style.display = "block";
}[/CODE]

If this is too hackish, did I understand you to mean making some sort of loop to cycle through each list item element with nextSibling?

Lastly, what do you mean by investing in a global?
Copy linkTweet thisAlerts:
@mjdamatoJan 22.2007 — For the global he means to use a global variable to keep track of what item is visible. So when you call the function it will 1) Hide the last displayed element (using the global variable), show the new element passed to the function, and set the global variable to the newly displayed element.

Or, you could just create a loop to go through all the child elements and use an if statement in the loop. If childElement == then new element (passed ot the function) make it visible, else make it hidden.
Copy linkTweet thisAlerts:
@NotionCommotionauthorJan 22.2007 — Thanks mjdamato,

I never learned about local and global variables until now. I just realized I was inadvertantly creating all global variables since I never used var inside my functions. Opps, time to change that. This spurs another question regarding what I should include in the main script, and what I should include in the onload function, but I will leave that for a separate post.

It turned out my second example inadvertantly did exactly what you two suggested regarding global variables. My solution to put a temporary element in my HTML to ensure that it is defined seems rather amaturish, however. Getting rid of it proved to be more difficult than I originally thought. I came up with the following, but it seems like it might have poor cross-browser acceptance. Is there a better way of doing it, or do I have it right? Thanks

[CODE]function hideDisplay(currMenu) {
if (!(typeof(thisMenu)=="undefined"))
{thisMenu.style.display = "none";}
thisMenu = document.getElementById(currMenu);
thisMenu.style.display = "block";}[/CODE]
Copy linkTweet thisAlerts:
@NotionCommotionauthorJan 23.2007 — I tried a different approach. Instead of checking if defined, I created a new object.

For the three approaches (1-create a temporary element in html, 2-check if the element is defined, and 3-create an object in js), are there any reasons to use one over another?

Thansk

[code=html]<head><script type="text/javascript">

thisMenu=new Object();
thisMenu.style=new Object();
thisMenu.id=null;
thisMenu.style.display=null;

function hideDisplay(currMenu) {
thisMenu.style.display = "none";
if (thisMenu.id!=currMenu)
{thisMenu = document.getElementById(currMenu);
thisMenu.style.display = "block";}
}
</script></head><body>

<a href="javascript:hideDisplay('id_1')">id_1</a>
<a href="javascript:hideDisplay('id_2')">id_2</a>
<a href="javascript:hideDisplay('id_3')">id_3</a>

<div id="id_1" style="display: none">id_1</div>
<div id="id_2" style="display: none">id_2</div>
<div id="id_3" style="display: none">id_3</div>
</body></html>[/code]
×

Success!

Help @NotionCommotion 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.23,
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,
)...