Hi stackoverflow community
I'm trying to work with the on change event of radio buttons that are created dynamically from an ajax call. These radios are created based on a MySQL result and each radio button should listen to an onChange event.
Here is my ajax call:
$.ajax({
...
success:function(ajax_result){
ajax_result.forEach(fn_Populate); // could be replaced with for loop
function fn_Populate(element){
$("xdiv").append("<input type='radio' id='radio_"+element[0]+"' name='xradio'>");
$("xdiv").append("<label for='radio_"+element[0]+"'>"+element[0]+"</label>");
}
}
});
This should create the radio buttons according to the ajax result, example radio_1, radio_2, radio_3, all with name xradio.
After the radio buttons are created, I need to identify when a button is clicked. Here is the code in jquery:
$("input[type=radio][name=xradio]").change(function(){
alert("DO SOMETHING");
// rest of code
});
The problem is that the change event is not fired if it is outside of the ajax call.
I need to fire it outside of the ajax call because it will generate another dynamic button through a new ajax call.
All help will be appreciated.
Thanks