views:

106

answers:

1

I've been at this for hours but it just isn't working. I'm trying to get ToggleJS working using Scriptaculous' Effects library. Specifically, the example: http://wiseheartdesign.com/page_attachments/0000/0075/demo.html.

I got it working beautifully using a pure html file called new.html. I then converted it to new.html.erb so I can incorporate some of the animations into my Rails project and it stopped working!

I tried both html and ruby javascript tags but neither are working, for example:

<%= javascript_include_tag "application", "prototype", "effects", "lowpro", "toggle" %>

The page's body is displaying on the browser, but without animations. I have made sure my javascript files are the same as the demo (plus, the html file is working with them anyway). Do you know where the problem might lie here with my .erb file?

A sample of the html I'm using:

<h3>Toggle.LinkBehavior</h3> 
    <div class="expand_me" id="one">Expanded!</div> 
    <p> 
      <a class="toggle" href="#one" rel="toggle[one]">Expand</a> 
    </p> 
    <div class="expand_me" id="two">Expanded! 1</div> 
    <div class="expand_me" id="three">Expanded! 2</div> 
    <p> 
      <a class="toggle" href="#two" rel="toggle[two,three]">Expand even more</a> 
    </p> 

#Application.js
Event.addBehavior({
   'a.toggle': Toggle.LinkBehavior(),
   'a.inverted_toggle': Toggle.LinkBehavior({invert: true}),
   'a.open': Toggle.LinkBehavior({
  effect: 'blind',
  onLoad: function(link) {
    this.toggle();
  },
  beforeToggle: function(link) {
    link.innerHTML = (link.innerHTML == "Open") ? "Opening..." : "Closing...";
  },
  afterToggle: function(link) {
    link.innerHTML = (link.innerHTML == "Opening...") ? "Close" : "Open";
  }
   }),
   'form input.checkbox.toggle': Toggle.CheckboxBehavior(),
   'form input.checkbox.inverted_toggle': Toggle.CheckboxBehavior({invert: true}),
   'form .radio_group.toggle': Toggle.RadioGroupBehavior({effect: 'blind'}),
   'form select.toggle': Toggle.SelectBehavior()
 });

Eimantas, I am a total newbie at this. I added to my html doc the following but it didn't work:

<script type="text/javascript">
window.onload = Event.addBehavior
</script>

Is this what you mean, or can you tell me what code I need to write in my .html.erb file?

+1  A: 

Can you paste generated html for javascript files?

I think your toggle.js is loaded later than application.js file, hence if application.js has all the hook code (window.onload...) for toggling effect it will fail since the library itself isn't loaded yet.

UPDATE:

Here's how you put Event.addBehavior to window loading:

<script type="text/javascripts">
  window.onload = function() {
    Event.addBehavior({
      // all params and code.
    });
  }
</script>

You should check prototype documentation on doing prototype style window.onload function.

Eimantas
Hi Eimantas, I tried switching the order just now but didn't work. All the HTMl I used is the same as the source code here: http://wiseheartdesign.com/page_attachments/0000/0075/demo.html. There's too much js and html for me to paste here but everything is in this link. I also added some detail to my question above. Does that give you the info you need?
sscirrus
It seems that your `addBehavior` method is called when html isn't loaded yet (i.e. first js is executed on unexisting elements, and then html elements are being loaded). Try adding `Event.addBehavior` call to `window.onload` function (or any appropriate Prototype wrapper function).
Eimantas
Hi Eimantas, I added some detail to the question. What do I need to do now?
sscirrus
I've updated my answer.
Eimantas
I think you meant <script type="text/javascript"> but it works! Thank you so much!
sscirrus