You are here:

DHTML/visible/hidden

Advertisement


Question
Dear Andrew,

I tried some scripts visible/hidden, diplay/none with div and table, but it work only in ie. In other browers my top menu (also in javascripts) doesn't show and my page layout is destroy...

Do you have andy suggestion for my calendar I am working on. I would like to make appear a description on click for some of the activity. See an exemple on my page: http://www.iqdho.com/neo/francais/activites.html and click on the link in the yellow cell... (Sorry it is only in French for the moment).

Can you help me with that?

Thanking you in advance for your prompt attention.

Answer
function show()
{
if (table.style.display=="none")
{
table.style.display="";
}
else
{
table.style.display="none";
}

Here, you are not referring to an 'id' value of any kind--that's why nothing is happening when you click the link.  Also, you seem to want to toggle the table div between visible and invisible.  However, in your logic, you don't really say that.  What we want to say is, "If the div with id 'table' is invisible, show it.  Otherwise, if the table is already visible, hide it.  Try:

function show()
{
// obtain reference to the id 'table'
var id = document.getElementById("table");
// if div display is hidden or unset, show it
if (id.style.display=="none" || id.style.display=="")
{
id.style.display="block";
}
else if (id.style.display=="block")
{ // if it's already shown, hide it
id.style.display="none";
}

}

Also, add a 'return false' in your function call.  That will keep the '#' from jumping us to the top of the page.

onclick="show()"

should be:

onclick="show(); return false"

--

Best of luck,
Andrew  

DHTML

All Answers


Answers by Expert:


Ask Experts

Volunteer


Andrew Hoffman

Expertise

Ask me about JavaScript, CSS, HTML5/XHTML, or PHP. Chances are I'll be able to help you. I know the economy is in the tank, but if you like my answer, please consider donating a few bucks. It's like tipping your barista, if your barista served fresh code instead of coffee.

Experience

I started doing this when boy bands were still cool.

Education/Credentials
I read a lot of nerdy books to get here.

©2012 About.com, a part of The New York Times Company. All rights reserved.