Prototype diagram / call from button

I thought this was pretty straight forward, but I am not getting the same results as the tutorials I read. I have a button on a html page that calls a function in script tags. I also have a link to a prototype.js file that I haven't started implementing yet. If you leave this link on the page, my function call won't work from the onclick event. Below the button's onclick event is called.

callIt = function(){

alert('It worked!');
}

</script>

      

0


source to share


2 answers


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

Then, once you are sure your page is correct, add the prototype.js library page in the page as the first script link:

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

      

Note that I did not close it, as if these <script ... />

script blocks should have an explicit closing tag (at least in the XHTML 1.0 Transitional)



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

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

      

Place this in script tags in the page element under the script prototype link. This will be done when the DOM is loaded and the button exists on the page.

Hope it helps.

+3


source


It worked. I'm just baffled as to why none of the examples I've worked with did this.



Thank!

0


source







All Articles