Hide and show div element with prototype and scriptaculous

36,793

Solution 1

maxnk already covered the main issue.

But, Prototype recommends using the dom:loaded event instead of window.onload:

document.observe("dom:loaded", bodyOnload);

Also, you might try Prototype's Element#toggle or Element#hide instead of Element#setStyle (unless the warning on each page applies to your CSS).

function bodyOnload() {
    $('content1').hide();
    $('content2').hide();
}

Solution 2

If you want to initially hide elements, its not enough to write the "function bodyOnload". This function should be called at page load by using something like that:

window.onload = bodyOnload;

And, of course, I advise you to give jQuery a try :)

Share:
36,793
hoyt.dev
Author by

hoyt.dev

We focus on the software so that you can focus on building more quality content: http://clientbucket.com http://twitter.com/clientbucket http://www.facebook.com/clientbucket

Updated on July 05, 2022

Comments

  • hoyt.dev
    hoyt.dev almost 2 years

    I'm trying to use prototype and scriptaculous to hide and display a div element but the function (below) to take advantage of the prototype setStyle property isn't working and I'm not sure what the problem is.

    <script type="text/javascript" language="javascript">
        function bodyOnload() {
                $('content1').setStyle({ display: 'none' });
                $('content2').setStyle({ display: 'none' });
        }
    </script>    
    
    
    
    <script type="text/javascript" language="javascript">
    
        var currentId = null;
    
        Effect.Accordion = function (contentId) {
            var slideDown = 0.5;
            var slideUp = 0.5;
    
            contentId = $(contentId);
    
            if (currentId != contentId) {
                if (currentId == null) {
                    new Effect.SlideDown(contentId, {duration: slideDown});
                    } else {
                    new Effect.SlideUp(currentId, {duration: slideUp});
                    new Effect.SlideDown(contentId, {duration: slideDown});
                }
                currentId = contentId; 
            } else {
                new Effect.SlideUp(currentId, {duration: slideUp});
                currentId = null;
            }
        };
        </script>
    

    The preceding function is called as such:

    <div id="accordion">
        <div id="part1">
            <div id="nav1" onclick="new Effect.Accordion('content1');">
                Post a comment 1
            </div>
            <div id="content1">
                <form id="form" name="form" action="post.php" method="post">
                <textarea name="commentbody" cols="20" rows="10"></textarea>
                <button type="submit">Post Comment</button>
                <input type="hidden" name="blogID" value="1" />
                <input type="hidden" name="userID" value="3" />
                <input type="hidden" name="parentID" value="7" />
                <div class="spacer"></div></form>
            </div>
        </div>
            <div id="part2">
            <div id="nav2" onclick="new Effect.Accordion('content2');">
                Post a comment 2
            </div>
            <div id="content2">
                <form id="form" name="form" action="post.php" method="post">
                <textarea name="commentbody" cols="20" rows="10"></textarea>
                <button type="submit">Post Comment</button>
                <input type="hidden" name="blogID" value="1" />
                <input type="hidden" name="userID" value="3" />
                <input type="hidden" name="parentID" value="7" />
                <div class="spacer"></div></form>
            </div>
        </div>
    </div>
    

    Here's what happens with the code.

    • In both ie and firefox it does nothing but when you click on the link that calls the effect.accordion method the method works as expected. The problem is with the prototype function which doesn't hide the elements. Any help with be greatly appreciated.