Search code examples
javascriptprototypejs

Prototypejs observe function throws error


I have this line of code but it throws an error. What is the problem with the code?

<html>
    <head>
        <script src="http://prototypejs.org/javascripts/prototype.js" type="text/javascript"></script>
        <script type="text/javascript">

            Event.observe('target', 'click', function(event) {
                // ...
            });

        </script>   
    </head>
    <body>
        <p id='target'>Click me!</p>
    </body>
</html>

Solution

  • Load the script after your

    tag is loaded. This way you dont need to detect the dom:loaded event. :)

    <html>
        <head>
            <script src="http://prototypejs.org/javascripts/prototype.js" type="text/javascript"></script>
    
        </head>
        <body>
            <p id='target'>Click me!</p>
              <script type="text/javascript">
    
                    Event.observe('target', 'click', function(event) {
                        alert('clicked me');
                    });
    
                </script>
        </body>
    </html>