Spoilers

Talk about computers, hardware, applications, and consumer electronics.
Member
Posts: 26
Joined: 2008.09.26 (15:18)
NUMA Profile: http://nmaps.net/user/Sockmonkey
MBTI Type: ISTJ
Location: England

Postby Sockmonkey » 2009.03.15 (18:29)

I'm making a web page and would very much like spoilers on it, but I have no idea how to make them. Would anyone be able to tell me how to make spoilers that expand/contract when certain text is clicked (as similar to the ones on the forums as possible)? I want many on a page so each one can't trigger the same hidden text, as what I tried before only opened the same spoiler no matter what I clicked.

Here is what I already had, although I expect it to be completely wrong (paste it into something like here to test it).

Code: Select all

<html><head>
<script type="text/javascript">function Spoiler(obj) { var inner = obj.parentNode.getElementsByTagName("div")[0]; if (inner.style.display == "none") inner.style.display = "";
else inner.style.display = "none"; }</script>
</head>
<body><div><input type="text" onclick="Spoiler(this);" value="Text" />
<div style="display:none;">Hidden text</div>
</div></body></html>
Thanks
I gave Snuggletummy a catgirl.
I gave Stephen a sperm.
I gave lord_day herpes.
I gave kirby a blowjob.
I gave Brttrx a steak.
I gave Sockmonkey a 220% warn. <3 -Blizz
Pawz gave me a sig.

User avatar
Damn You're Fine
Posts: 384
Joined: 2008.09.19 (01:47)
NUMA Profile: http://nmaps.net/user/littleviking001
MBTI Type: INTP
Location: Southern California
Contact:

Postby LittleViking » 2009.03.15 (23:14)

Yeah, but your code is exactly right. o_O Here's what I was testing. Works perfectly:

Code: Select all

<html>
<head>
  <script type="text/javascript">
    function Spoiler(obj)
    {
      var inner = obj.parentNode.getElementsByTagName("div")[0];
      if (inner.style.display == "none") {
        inner.style.display = "";
      } else {
        inner.style.display = "none";
      }
    }
  </script>
</head>
<body>
  <div>
    <input type="text" onclick="Spoiler(this);" value="Text1" />
    <div style="display:none;">Hidden text</div>
  </div>
  <div>
    <input type="text" onclick="Spoiler(this);" value="Text2" />
    <div style="display:none;">Hidden text</div>
  </div>
  <div>
    <input type="text" onclick="Spoiler(this);" value="Text3" />
    <div style="display:none;">Hidden text</div>
  </div>
</body>
</html>
Image
The last Metroid is in captivity. The galaxy is at peace...

The number of Electoral College votes needed to be President of the US.
Posts: 282
Joined: 2008.10.07 (04:17)
NUMA Profile: http://nmaps.net/user/Fraxtil
MBTI Type: INTJ
Location: Arizona, USA
Contact:

Postby Fraxtil » 2009.03.15 (23:52)

Why do you use an input field to do this? Wouldn't this work with a <p>aragraph as well?


Who is online

Users browsing this forum: No registered users and 22 guests