Accessing new-created DOM elements
ive got preblem with selecting element created during work on page. my code looks like:
-
$("button").click(function() {
alert("boo!");
});
$("div").append("<button>make a boo</button>");
as you might see (and if i get it right), in time when selector is looking for a button in the page code, none is created yet, so it won't assemble it any click action.
i've found this a bit problem in my case, when i'm loading data from db and making a list from them. i need to acces element in this list. so i've manage to "reload" the selector to "rescan" the actual code, when new element are created, by putting them into a function:
-
function makeBooing() {
$("button").click(function() {
alert("boo!");
});
}
$("div").append("<button>make a boo</button>");
...
makeBooing();
it works well, but now i need some event that "reload" the code when new element will appear. i can't reload it right from "appending" section cos i need it to be realoaded as well and then it'll cause an infinit iteration. i was thinking about using change() on DIV which holds new-created elements, but it's not working right for me, or more likely i'm not using it right way. also it isn't the very clean way to handle this at all.
can anyone help please?