I am creating some buttons dynamically and assigning them IDs.
When ever someone clicks that button I want to collect the ID and from there perform some task.
I did this using delegate().
In my case, a PHP script generates the content of a page from a MySQL database. To each element, a button is added (clicking these buttons can remove the respective element from the database). The removal is handled by another PHP script which these buttons activate, so a click event is attached to each button like this:
$('.items').delegate('[type="button"]', 'click', remove_item);
where the buttons are part of the items class and remove_item is the callback for the removal.