Assume I have some jQuery code that connects an event handler to all elements with class
.myclass
.
For example:*
$(function(){
$(".myclass").click( function() {
// do something
});
});
What's more, my HTML may be as per the following:
<a class="myclass" href="#">test1</a>
<a class="myclass" href="#">test2</a>
<a class="myclass" href="#">test3</a>
That works with no issue. Be that as it may, consider if the .myclass
elements were written to the page at some future time.
*For example:
<a id="anchor1" href="#">create link dynamically</a>
<script type="text/javascript">
$(function(){
$("#anchor1").click( function() {
$("#anchor1").append('<a class="myclass" href="#">test4</a>');
});
});
</script>
Essentially, I might want to write the click() overseer once and have it apply to both content present at page load, and content acquired later by means of AJAX/DHTML. Any thought how I can fix this?