How to not use JavaScript with in the elements events attributes but still load via AJAX
Posted
by thecoshman
on Stack Overflow
See other posts from Stack Overflow
or by thecoshman
Published on 2010-04-28T08:04:04Z
Indexed on
2010/04/28
8:13 UTC
Read the original article
Hit count: 272
I am currently loading HTMl content via AJAX.
I have code for things on different elements onclick
attributes (and other event attributes).
It does work, but I am starting to find that the code is getting rather large, and hard to read. I have also read that it is considered bad practice to have the event code 'inline' like this and that I should really do by element.onclick = foobar
and have foobar
defined somewhere else.
I understand how with a static page it is fairly easy to do this, just have a script tag at the bottom of the page and once the page is loaded have it executed. This can then attach any and all events as you need them.
But how can I get this sort of affect when loading content via AJAX. There is also the slight case that the content loaded can very depending on what is in the database, some times certain sections of HTML, such as tables of results, will not even be displayed there will be something else entirely.
I can post some samples of code if any body needs them, but I have no idea what sort of things would help people with this one. I will point out, that I am using Jquery already so if it has some helpful little functions that would be rather sweet¬
© Stack Overflow or respective owner