0

I have about 40 questions on a Question/Anwser script now I want to hide all the other messages then the one with the id clicked but the windows overlap, So how would I hide all the Other ids and show just one with Javascript?

1 Answer 1

3

The following examples will vary depending on your HTML structure. They are meant as examples, not solutions for your specific situation.

Changing the style of a single element can be done rather quickly:

document.getElementById("foo").style.display = "none";

You could hide all others like this:

var myDivs = document.getElementsByTagName("DIV");
for (var i = 0; i < myDivs.length; i++) {
  if (myDivs[i].id != "foo") {
    myDivs[i].style.display = "none";
  } else {
    myDivs[i].style.display = "block";
  }
}

If you're open to using a framework, like jQuery, you could do the same thing even quicker:

$(".question .title").click(function(){
 $(this)
   .next().slideDown()
   .closest(".question").siblings()
   .find(".body").slideUp(); 
});​

Online Demo of jQuery: http://jsbin.com/opisa/edit

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.