I'm trying to dynamically create portfolio parts for my website in jQuery so I can add stuff later to it with ease through the database. I've got everything working, except the listener always redirects to the last listener created, it seems. So no matter which header I click, it always opens the last option, not the one clicked. Here's the code I have:
var portfolio_array = <?php echo json_encode($GLOBALS["portfolio_array"]); ?>;
for (var i = portfolio_length - 1; i >= 0; i--) {
var project_name = portfolio_array[i]["name"];
var project_tag = portfolio_array[i]["tag"];
var project_description = portfolio_array[i]["description"];
var project_year = portfolio_array[i]["year"];
var project_image = portfolio_array[i]["image"];
$("projects").append("<project class = " + project_tag + ">");
$("."+project_tag).append("<br><project_header class = project-header>" + project_name + "</project_header>");
$("."+project_tag).append("<br><project-content class = slideable></project-content>");
$("."+project_tag).children(".slideable").append("<project_description class = project-desc>"+project_description+"</project_description>");
$("."+project_tag).children(".slideable").append("<br><project_image class = project-image>"+project_image+"</project_image>");
//<img src="pic_mountain.jpg" alt="Mountain View" style="width:304px;height:228px;">
$("."+project_tag).children(".slideable").hide(1000);
//alert("Added a click listener to tag " + project_tag);
$("."+project_tag).children(".project-header").click(function(event){
$("."+project_tag).children(".slideable").slideToggle(400);
//alert("Clicked " + project_tag);
});
};
I've tried debugging it through alerts, but couldn't find anything wrong with it other than the click alert always pointing to the last project created. The alert before the creation of the event is giving me the correct project names.
Anyone see what's wrong with this? I'm still kind of new to this, so if there might be a really stupid mistake somewhere in there that I don't know of yet...