You can fix this two different ways: either move your script block to the bottom of the page (just before /body, as in my example) or add an unobtrusive listener block around your main function. The issue here is that because your script appears before the HTML it applies to, none of the elements are present in the DOM yet when the closure tries to wrap around them.
Here's how to wrap them (if you want to put the script at the top of the page: <script type="text/javascript"> document.observe('dom:loaded', function(){ var toggles = $$('.toggle'). [etc...] }); </script> Walter On Dec 19, 2012, at 8:13 AM, tab1ta wrote: > Taking from an example that works: > > <!DOCTYPE html> > <html> > <head> > <meta charset="utf-8" /> > <title>Show-Hide Divs</title> > <script type="text/javascript" > src="http://ajax.googleapis.com/ajax/libs/prototype/1.7/prototype.js"></script> > [snip] > toggles = $$('.toggle').invoke('hide'); > }) > </script> > </body> > </html> > > I tried to apply this script to a complete webpage but i trows me an error > TypeError the $('') function considers the id i pass to it as a null. My web > page is longer. > > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> > <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> > <head> > <style type="text/css"> > @import url(styles.css); > > </style> > <script type="text/javascript" > src="http://ajax.googleapis.com/ajax/libs/prototype/1.7/prototype.js"></script> > <script type="text/javascript"> > var toggles = > $$('.toggle').invoke('hide'); [snip] > <div id="thing_6" > class="toggle"> > <p>© by > flicr.com<br/><a href="http://tinyurl.com/cwtt2yq">See Photo</a></p> > </div></div></div></div> > > </body> > </html> > > > -- > You received this message because you are subscribed to the Google Groups > "Prototype & script.aculo.us" group. > To view this discussion on the web visit > https://groups.google.com/d/msg/prototype-scriptaculous/-/uxHIYaSumLsJ. > To post to this group, send email to prototype-scriptaculous@googlegroups.com. > To unsubscribe from this group, send email to > prototype-scriptaculous+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/prototype-scriptaculous?hl=en. -- You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com. To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en.