[eluser]Phil Sturgeon[/eluser]
Posting a jQuery issue here as I know plenty of you are great with the framework. Well, that and I already have this forum bookmarked ;-)
I'm creating a 3 leveled dynamic menu system and the damn thing will be the death of me. It seems that HTML I load with JS (AJAX call returning JSON made into HTML with jQuery) wont listen to the same rules as HTML loaded through a normal PHP echo.
Code:
Code:
<h2>Browse</h2>
<ul id="submenu">
<? foreach($this->geolocation_m->getCountries(array('has_profiles'=>true)) as $country): ?>
<li>
<?=anchor('profiles/browse/country/'.$country->code, $country->name, array('id'=>'country-'.$country->id)); ?>
<? if(isset($result_country) && $result_country->code == $country->code): ?>
<ul id="cities-<?=$result_country->code; ?>" class="populated">
<? foreach($menu_cities as $city): ?>
<li><a >id ?>" href="[removed]void(0);<?//=site_url('profiles/browse/city/'.$city->id); ?>"><?=$city->name; ?> (<?=$city->profile_count;?>)</a></li>
<? endforeach; ?>
</ul>
<? endif; ?>
</li>
<? endforeach; ?>
</ul>
[removed]
$(function() {
$("ul#submenu li a[id^='country-']").click(function() {
// Hide all other populated lists
$(this).parent('li').siblings('li').children('ul.populated').slideUp();
// If this country already has a list of cities, then show it
if($(this).siblings('ul').children('li').length > 0) {
$(this).siblings('ul').slideDown();
// Not yet populated, get the data
} else {
// Get UK from country-UK
country = this.id.replace('country-', '')
// Set a new list element. Eg, country-UK clicked sets new ul as cities-UK
city_list = $('<ul/>').attr('id', 'cities-' + country).addClass('populated');
populate_city_list(city_list, country, '<a/>');
$(this).parent().append(city_list);
}
return false;
});
// Populate activities
$("a[id^='city-']").click(function() {
// Set all other populated lists to be hidden
$(this).parent('li').siblings('li').children('ul.populated').slideUp();
// If this country already has a list of cities, then show it
if($(this).siblings('ul').children('li').length > 0) {
$(this).siblings('ul').slideDown();
// Not yet populated, get the data
} else {
// Get 2342 from city-2342
city = this.id.replace('city-', '')
// Set a new list element. Eg, city-2342 clicked sets new ul as activities-2342
activity_list = $('<ul/>').attr('id', 'activities-' + city).addClass('populated');
populate_activities_list(activity_list, city, '<a/>');
$(this).parent().append(activity_list);
}
return false;
});
});
[removed]
This is not the cleanest code here, but I have reason for my madness.
Anyway, it loads a list of countries with a normal PHP loop, and can possibly show cities if some have already been selected in PHP.
Normally a user will click a cuntry, be shown a list of cities, then should be shown a list of activities.
INSTEAD what happens is a user clicks a country, is shown a list of cities then when they click one, instead of the jQuery rule being listened to, it takes them to the city result page. They click again on the same city and THEN they are shown the list of activities.
Any idea how I can get my dynamically inserted HTML playing by the same rules?