Create an event dyanmically, after reading a text file
I am trying to setup a html which performs the following:
read a text file from the server
using data in that text file, create dynamically buttons in the html page (say, one button per line)
assign a handler for the click event in every button, triggering the same function for all of them, but changing the data passed.
I got to work if I create the buttons without reading from the server, so using a internal variable that handles the values .... but, when getting to the file, I am able to read the file, create the buttons,... and then fail to create the events.
the code to read is this
$.ajax({
type: 'GET',
url: './get-csv.php',
data: null,
success: function(text) {
CreateButtons(idOrigin);
CreateEvents(idOrigin);
}
});
the CreateEvents uses this
var toBrowse = '#' + idOrigin + ' button';
for (var i = 0; i < $(toBrowse).length , i++) {
$(toBrowse).eq(i).on('click', {value: i}, function(event) { // NEVER WORKS!!!!!
// var msgs = [
// "button = " + $(this).index(),
// "event.data.value = " + event.data.value,
// "i = " + i
// ];
alert('hello');
}
}
which always fails in the $(toBrowse...
I wonder if the fact that the CreateEvents routine is invoked WITHIN the success part of the get has something to do for it never working
thanks