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.
[07:46 am]
RIVE -- :wob:
[2024-4-22. : 6:48 pm]
Ultraviolet -- :wob:
[2024-4-21. : 1:32 pm]
Oh_Man -- I will
[2024-4-20. : 11:29 pm]
Zoan -- Oh_Man
Oh_Man shouted: yeah i'm tryin to go through all the greatest hits and get the runs up on youtube so my senile ass can appreciate them more readily
You should do my Delirus map too; it's a little cocky to say but I still think it's actually just a good game lol
[2024-4-20. : 8:20 pm]
Ultraviolet -- Goons were functioning like stalkers, I think a valk was made into a banshee, all sorts of cool shit
[2024-4-20. : 8:20 pm]
Ultraviolet -- Oh wait, no I saw something else. It was more melee style, and guys were doing warpgate shit and morphing lings into banelings (Infested terran graphics)
[2024-4-20. : 8:18 pm]
Ultraviolet -- Oh_Man
Oh_Man shouted: lol SC2 in SC1: https://youtu.be/pChWu_eRQZI
oh ya I saw that when Armo posted it on Discord, pretty crazy
[2024-4-20. : 8:09 pm]
Vrael -- thats less than half of what I thought I'd need, better figure out how to open SCMDraft on windows 11
[2024-4-20. : 8:09 pm]
Vrael -- woo baby talk about a time crunch
[2024-4-20. : 8:08 pm]
Vrael -- Oh_Man
Oh_Man shouted: yeah i'm tryin to go through all the greatest hits and get the runs up on youtube so my senile ass can appreciate them more readily
so that gives me approximately 27 more years to finish tenebrous before you get to it?
Please log in to shout.


Members Online: Roy