Staredit Network > Forums > Technology & Computers > Topic: Collapse Boxes
Collapse Boxes
Feb 6 2013, 11:40 am
By: Jack  

Feb 6 2013, 11:40 am Jack Post #1

>be faceless void >mfw I have no face

Hi everyone,

I'm currently working on a website and would like to know a better way to do collapse boxes than the way I am currently. As far as I can see, the way SEN's collapse boxes work is that every time the BBcode parser finds [Collapse], it generates a new col_12312312523523 or similiar, so that each collapse box is uniquely identified. Basically I'd like to have a look at exactly how SEN does it, or else some advice on how to do it from others who have. I know how to make a collapse box, but it's the unique identifiers and how the page knows which ones are which from just the BBCode parsing that I'd like help on.



Red classic.

"In short, their absurdities are so extreme that it is painful even to quote them."

Feb 6 2013, 3:45 pm Biophysicist Post #2



In your <head> tag, add the following:

Code
<script type="text/javascript">
function toggleBox(button) {
var cbox = button.parentNode.getElementsByTagName("div")[0];
if(cbox.style.display == "block")
cbox.style.display = "none";
else
cbox.style.display = "block";
}
</script>


Now, for your BBCode, you'll want the following structure:
Code
<div class="cbox">
<input type="button" onclick="toggleBox(this);" value="Toggle Collapse Box" />
<div style="display:none;">blah blah contents go here blah</div>
</div>


Untested, but I'm confident it'll work.

EDIT: If you're using someone else's forum software and can't easily modify the <head> tag, which is sometimes the case, then you can do the JS inline if needed. But that's dumb.



None.

Feb 6 2013, 8:32 pm Dem0n Post #3

ᕕ( ᐛ )ᕗ

You should make it so that clicking anywhere on the box opens it, instead of having to click to on a small button in the corner.




Feb 6 2013, 9:36 pm Jack Post #4

>be faceless void >mfw I have no face

Bio, thanks very much for your help!

Dem0n, yeah I can do that, good idea.



Red classic.

"In short, their absurdities are so extreme that it is painful even to quote them."

Feb 7 2013, 10:16 pm ShadowFlare Post #5



I seem to recall seeing the source for a page that used CSS to apply the onclick handler so you would only need to assign the class to the HTML element. I don't remember exactly what it looked like, though.



None.

Feb 8 2013, 12:38 am Roy Post #6

An artist's depiction of an Extended Unit Death

CSS is supposed to be for styling and layout, not behavior. I did a quick search for CSS click events and I only really saw clever little hacks than a native implementation: it seems more academic than practical.

On that note, you shouldn't couple your HTML to your JavaScript; the HTML shouldn't know about it. The only real criticism I have for Bio's approach is that he's calling the JS method from the HTML (which is a common practice, but not a good one, in my opinion). Here's how I would change it:

Code
<script type="text/javascript">
   $.(
       $('.cbox').on('click', '.cbox-header', function(e) {
           if($(e.target).is('span')) {
               return false; // Ignore clicks on span text
           }
           $(this).parent().children('.cbox-content').toggle();
       });
   );
</script>
I used jQuery, by the way. I would recommend it.

Code
<div class="cbox">
   <div class="cbox-header">
       <span>Collapse Box</span>
       <button>+</button>
   </div>
   <div class="cbox-content" style="display:none;">blah blah contents go here blah</div>
</div>

Changed the input button to just a regular button. Otherwise, basically the exact same idea.

See this JsFiddle for a sample implementation of the above (I stole basically all of the CSS styling from Sen). I even made it expand when you click anywhere on the header (except the span text) like Demon suggested.

Edit: Made it better.

Post has been edited 8 time(s), last time on Feb 8 2013, 2:53 am by Roy.




Options
  Back to forum
Please log in to reply to this topic or to report it.
Members in this topic: None.
[2024-4-14. : 9:21 pm]
O)FaRTy1billion[MM] -- there are some real members mixed in those latter pages, but the *vast* majority are spam accounts
[2024-4-14. : 9:21 pm]
O)FaRTy1billion[MM] -- there are almost 3k pages
[2024-4-14. : 9:21 pm]
O)FaRTy1billion[MM] -- the real members stop around page 250
[2024-4-14. : 9:20 pm]
O)FaRTy1billion[MM] -- look at the members list
[2024-4-12. : 12:52 pm]
Oh_Man -- da real donwano
da real donwano shouted: This is the first time I've seen spam bots like this on SEN. But then again, for the last 15 years I haven't been very active.
it's pretty common
[2024-4-11. : 9:53 pm]
da real donwano -- This is the first time I've seen spam bots like this on SEN. But then again, for the last 15 years I haven't been very active.
[2024-4-11. : 4:18 pm]
IlyaSnopchenko -- still better than "Pakistani hookers in Sharjah" that I've seen advertised in another forum
[2024-4-11. : 4:07 pm]
Ultraviolet -- These guys are hella persistent
[2024-4-11. : 3:29 pm]
Vrael -- You know, the outdoors is overrated. Got any indoor gym and fitness equipment?
[2024-4-10. : 8:11 am]
Sylph-Of-Space -- Hello!
Please log in to shout.


Members Online: jjf28