script.aculo.us Effect.toggle.(’MyAss’)

All, Javascript, Prototype, Scriptaculous

For one of my clients, i had to clean up some mess which was done by another college. The web site has a registration form and a login form which is both in the same screen. This makes the screen look too big and too complicated. There were also some fields and forms which are at wish if you only choose specific options.

So i decided to make the form look smaller. I wanted to hide some fields until they are not checked or used. I stared to program at 21:40.

1. Scenario 21:50 (after some cigarets and jagermeister)

I have some background experience with http://mootools.net and http://script.aculo.us. Because I allready use scriptticolumous in the web site i decided to use scriptaulimoumus but I still don’t know how to spell it or read (always have to search on google.com that he corrects my misspelling).

After like an half an hour reading i tried to use the toggle function. And some code which i would write would look like:

  1. <table width="100%" border="0" cellspacing="0" cellpadding="0">
  2.   <tr>
  3.     <td><input type="checkbox" name="checkMeToSeHiddenFields" id="checkMeToSeHiddenFields" onclick="Effect.toggle(’content_to_show’, ’slide’)"> Click me to see something</td>
  4.   </tr>
  5.   <tr>
  6.     <td><div id="content_to_show" style="display:none">
  7.                 <div><input type="text" name="noname" /></div>
  8.                 </div></td>
  9.   </tr>
  10.   <tr>
  11.         <td><input type="submit" value="submit" /></td>
  12.   </tr>
  13. </table>
  14. </form>

I think this is pretty straight forward. What it does, on the beginning the checkbox is not checked and the div is not shown so if you would click on the checkbox it would be checked and the layer would be shown. Basiclly it works, if you are smart and with no adrenalin and no stress. But for me no way, as soon as i see something to click i would click on it like 5 billion times a second to make it crash or to relax. So after clicking a few million times i saw that the checkbox was checked but the div was not shown and some more million clicks the checkbox was not checked and the div was shown. :-( no shit.

2. Scenario 22:30(some more cigarets and …)

What now? I decided to handle the toggle function on my own. Now the new code:

JAVASCRIPT [Show Plain Code]:
  1. <script type="text/javascript">
  2. function toggleSomeEffect() {
  3.         if($(‘checkMeToSeHiddenFields’).checked == true) { // the checkbox to check
  4.                 if($(‘content_to_show’).visible()) { // if the div is visible do nothing
  5.                
  6.                 } else { // else show the div.
  7.                         Effect.SlideDown(‘content_to_show’);
  8.                 }
  9.         } else { // not checked so dont show it
  10.                 if($(‘content_to_show’).visible()) { // if div visible hide it
  11.                         Effect.SlideUp(‘content_to_show’);
  12.                 }
  13.         }
  14. }
  15. </script>
  1. <table width="100%" border="0" cellspacing="0" cellpadding="0">
  2.   <tr>
  3.     <td><input type="checkbox" name="checkMeToSeHiddenFields" id="checkMeToSeHiddenFields" onclick="toggleSomeEffect()"> Click me to see something</td>
  4.   </tr>
  5.   <tr>
  6.     <td><div id="content_to_show" style="display:none">
  7.                 <div><input type="text" name="noname" /></div>
  8.                 </div></td>
  9.   </tr>
  10.   <tr>
  11.         <td><input type="submit" value="submit" /></td>
  12.   </tr>
  13. </table>
  14. </form>

I thought this might work and started to click on it 5 billion times a second same thing they are not syncronized as they should be. :-( kiss my ass.

3. Scenario 11:50 (more smoke)

There are some more smaller in scenarios in the story but will just skip them not make it so exciting huh. So i decided to make a fuse which would tell me if everything is allready done. So this is the last one.

I started to read the callback functions and found beforeStart and afterFinish. So would before start make a variable true and after finish make the variable false. In that way i would know when i could make another Effect call. in the afterFinish callback function i would also implement a checking for the checkbox which would tell me if it is checked or not and show the div or not. In this way they will be always syncronized.

JAVASCRIPT [Show Plain Code]:
  1. <script type="text/javascript">
  2. var finito = true; // our fuse variable
  3.  
  4. function beforeStartCallMe(obj) { // this gives you the div object obj.
  5.         finito = false; // we started an Effect.
  6. }
  7.  
  8. function afterFinishCallMe(obj) {
  9.         finito = true; // the effect is finished.
  10.         if($(‘checkMeToSeHiddenFields’).checked == true) { // if checked.
  11.                 if($(‘content_to_show’).visible() == false) { // and div not visible
  12.                         Effect.SlideDown(‘content_to_show’, {beforeStart: beforeStartCallMe, afterFinish: afterFinishCallMe});
  13.                 }
  14.         } else {
  15.                 if($(‘content_to_show’).visible()) { // if visible div but not checked checkbox
  16.                         Effect.SlideUp(‘content_to_show’, {beforeStart:beforeStartCallMe, afterFinish: afterFinishCallMe});
  17.                 }
  18.         }
  19. }
  20.  
  21. function toggleSomeEffect() {
  22.         if($(‘checkMeToSeHiddenFields’).checked == true) {           
  23.                 if($(‘content_to_show’).visible()) {
  24.                 } else {
  25.                         if(finito) {
  26.                                 Effect.SlideDown(‘content_to_show’, {beforeStart: beforeStartCallMe, afterFinish: afterFinishCallMe});
  27.                         }
  28.                 }
  29.         } else {
  30.                 if($(‘content_to_show’).visible() && finito) {
  31.                         Effect.SlideUp(‘content_to_show’, {beforeStart: beforeStartCallMe, afterFinish: afterFinishCallMe});
  32.                 }
  33.         }
  34. }
  35. </script>
  1. <table width="100%" border="0" cellspacing="0" cellpadding="0">
  2.   <tr>
  3.     <td><input type="checkbox" name="checkMeToSeHiddenFields" id="checkMeToSeHiddenFields" onclick="toggleSomeEffect()"> Click me to see something</td>
  4.   </tr>
  5.   <tr>
  6.     <td><div id="content_to_show" style="display:none">
  7.                 <div><input type="text" name="noname" /></div>
  8.                 </div></td>
  9.   </tr>
  10.   <tr>
  11.         <td><input type="submit" value="submit" /></td>
  12.   </tr>
  13. </table>
  14. </form>

Now this it. The time is 3:00 i am half drunk but the thing work. I does not mess up the screen and they stay syncronized.

Some about me, i am not a javascript programmer and as a programmer I suck. There are for sure some shorter ways to to this, feel free to send them to me i will publish them and learn from them. I go sleep. :-) damn I am good.

3 Responses to “script.aculo.us Effect.toggle.(’MyAss’)”

  1. Justin Says:
    January 29th, 2008 at 06:15:15

    I know this is a late comment, but do new Effect.toggle(’mydiv’,'Blind’,{limit:1});

    :)

  2. orange_goat Says:
    February 16th, 2008 at 12:03:12

    before, thank a lot for your share dude :D

    but…

    arrrggghh!! it isn’t working!!! pleassseee…..

    i wrote the second code, but it doesn’t work for me…

  3. merlin Says:
    January 21st, 2009 at 11:54:02

    this is exactly what i was looking for.

    works perfect, thx

Leave a Reply

Icons by N.Design Studio. Designed By Ben Swift. Powered by WordPress and Free WordPress Themes