I am trying to load a dropdown menu from a server and then use jQuery to interact with that dropdown. Everything loads fine, but I can't interact with the menu items as they weren't part of the original HTML that was loaded, they were entered after AJAX got its response.
Here's the ajax code:
$(document).ready(function(){
//load dropdown menu using ajax
$.ajax({ url: "default/CallSupplierMongo",
context: document.body,
success: function (res) {
document.getElementById("dropdownItems").innerHTML = res;
}
});
});
The jQuery:
$(document).load(function() {
$('.dropdownItems > div').click(function () {
supplierCode = $(this).html();
$('.dropdownItems > div').css({
'background-color': 'white',
'color': '#888888'
});
$(this).css({
'background-color': '#888888',
'color': 'white'
});
$('.dropdownItems').slideUp('fast');
$('.dropdownButton').html(supplierCode);
$('.dropdownButton').css({
'border-bottom-right-radius': '5px',
'border-bottom-left-radius': '5px'
})
});
});
And the PHP:
function actionCallSupplierMongo() {
self::getSuppliers();
}
private static function echoSupplierCodes($supplierCodes) {
for ($i=0;$i<count($supplierCodes);++$i) {
echo '<div>'.$supplierCodes[$i].'</div>';
}
}
To explain the issue further: I want to click on the dropdown items and grab the HTML inside it. When I click, nothing is registered. I believe that's because jQuery checks to see if those <div>
s exist before they are loaded and thus doesn't apply the .click
function to them.
EDIT I've tried putting the AJAX call in $(document).load()
but it still has no effect.