I am trying to do go through the following scenario:
- User selects an employee (option) on a select element.
- User double clicks the option / clicks on a button that calls a function with a POST request to the server.
- The POST returns a string with JSON (containing a piece of HTML with the structure for a new select element).
- The POST callback executes two functions: one that loads an array with the JSON content and another that replaces the old select (containing a list of employees) with the new select brought by the POST.
Until this point everything works perfectly fine.
- The new select is filled with the options that are stored in an array (containing the data brought by the POST as JSON data).
I need to automate this. I can already make it work by using a click event on a new button that is generated, but my intent is to load the new select with its content loaded.
I don't want to generate the new select containing its content already because as the user interacts with the content will be changed. There's no need to duplicate the code to fill the select both in the jQuery and in the PHP. It's much simpler just to use jQuery all the time to generate the options for the select.
I tried the parameter "complete" but it still doesn't work.
EDIT: I created an js fiddle: https://jsfiddle.net/bmv35vwz/
My code:
function load_edituser(){
iduser = $("select.showallusers option:selected").val();
name = $("select.showallusers option:selected").text();
$.ajax({'url': base_url+'/ManageUser/open_edituser',
'type': 'POST',
'data':{'iduser': iduser, 'name': name},
'success' : function(data){
var container = $('.usermanagement-canvas');
if(data)
{
get_user_apps(); //it loads the array with the JSON data, it's another POST function.
container.html(data);
}
else
alert('Error!!');
},
'complete': function()
{
display_user_apps(); //This is the function that fills the select with options, not being called here.
}
});
}
function get_user_apps(){
//alert("it works! name: "+name+" id: "+iduser);
$.ajax({'url': base_url+'/ManageUser/get_json_user_apps',
'type': 'POST',
'data':{'iduser': iduser, 'name': name},
'success' : function(data){
//alert("Hell yes");
//var container = $('.usermanagement-canvas');
if(data)
{
appsinfo = JSON.parse(data);
}
else
alert('Error!!');
}
});
}
function display_user_apps()
{
//alert(appsinfo.currentapps.length);
//alert("Shut up");
var i;
for (i = 0; i < appsinfo.currentapps.length; i++)
{
var id = appsinfo.currentapps[i].idApplication;
var appName = appsinfo.currentapps[i].appName;
currentlist += "<option value="+id+">"+appName+"</option>";
}
$("select.left-current").html(currentlist);
for(i=0; i < appsinfo.excludedapps.length; i++)
{
var id = appsinfo.excludedapps[i].idApplication;
var appName = appsinfo.excludedapps[i].appName;
notallowedlist += "<option value="+id+">"+appName+"</option>";
}
$("select.right-excluded").html(notallowedlist);
}
EDIT
I am aware of delegating events. I am using that for the button work:
$(".usermanagement-canvas").on("click","button.testbutton",display_user_apps);
However, I DON'T want to use a button. I want to the trigger the function automatically right after the AJAX changes the div's html. In this case, I'd need some sort of event that detects the HTML being changed.