I have the below code, the first HTML snippet is loaded on the initial page load, then when the span with id flag is clicked the javascript will load the second HTML snippet into a hidden div on the webpage.
Now this hidden div has a select drop down box which when changed the javascript should make another AJAX call but $("#overlay #innerWrapper #country").on('change', function() {
is never called.
I'm sure it's an event delegation issue but can't seem to see where I'm going wrong!?
$("#topBar").on("click", "#flag", function(e) {
e.preventDefault();
var data = {};
$("#overlay").css("display", "inline");
// Country Change
$("#overlay #innerWrapper #country").on('change', function() {
var country = $(this).val();
ajaxCall(country);
});
ajaxCall(data);
e.stopPropagation();
});
<div id="topBar">
<span id="flag" class="flag-icon"></span>
</div>
<div id="innerWrapper">
<div id="title">Select Country</div>
<span id="flag" class="flag-icon"></span>
<select id="country" name="country">
<option value="fr" selected="selected"> France</option>
<option value="de"> Germany</option>
</select>
</div>
$("#innerWrapper").on('change', '#country', function() {
alert('1');
var country = $(this).val();
ajaxCall(country);
});
$("#topBar").on("click", "#flag", function(e) {
e.preventDefault();
var data = {};
$("#overlay").css("display", "inline");
ajaxCall(data);
e.stopPropagation();
});
function ajaxCall(element, callId, dataIn)
{
var limit;
var data;
if ($(element).data("limit")) {
limit = $(element).data("limit");
data = {id: callId, limit: limit, data: dataIn};
}
data = {id: callId, data: dataIn, element: element};
$.ajax(
{
type: "POST",
url: "ajax.php",
data: data,
beforeSend: function ()
{
if (element != 'ignore') {
$(element).append( "<div class=\"loading\"></div>");
}
},
success: function (data)
{
if (element != 'ignore') {
$(element).html(data);
} else {
location.reload(true);
}
},
complete: function ()
{
if (element != 'ignore') {
$(element).siblings(".loading").remove();
}
},
error: function (jqXHR)
{
$(element).html(jqXHR.status + " - " + jqXHR.statusText);
}
});
}