tags:

views:

116

answers:

2

I thought this was pretty straight forward but I don't get teh same results as the tutorials I read. I have a button on an html page that calls a fucntion in script tags. I also have a reference to the prototype.js file which I haven't even begun to implement yet. If I leave that reference in the page, my function call does not work from the button's onclick event. Below is what is called from the button onclick event.

callIt = function(){

alert('It worked!');
}

</script>
+3  A: 

A couple of things: first, make sure your HTML is valid. Run it through the validator at http://validator.wc.org.

Next, once you're sure that your page is valid, add the prototype.js library as the first script reference on the page:

<script type="text/javascript" src="prototype.js"></script>

Notice that I didn't close it like this <script ... /> Script blocks need to have an explicit closing tag (at least in XHTML 1.0 Transitional)

Now, to answer your question, I'm really not sure what you're asking, but if you wanted to attach the callIt method to the onclick handler of your button using Prototype, then do this:

Event.observe(window, 'load', function() {
    Event.observe('button_id', 'click', callIt);  
});

Put this in script tags in the element of the page, below the prototype script reference. This will execute when the DOM is loaded & the button exists on the page.

Hope this helps.

Ben Scheirman
A: 

That worked. I'm just puzzled why none of the examples I have been working from have done this.

Thanks!

CountCet