I have divs created within a while loop displaying a MySQL query. I’d like to hide and show them with .slideToggle. I can’t use one class as that would trigger every specific div on the page to slide down.
I thought this would repeat within the while loop and find the closest toggleSectionDyn div id. Obviously not.
<div class="actions"><a href="#" id="toggleButtonDyn">Add</a></div>
<div id="toggleSectionDyn">Some content</div>
<script>
$("#toggleButtonDyn").click(function(){
$(this).closest("#toggleSectionDyn").slideToggle("slow");
return false;
});
</script>
EDIT: live is deprecated. consider using .on()
you want to use
.live()from the .live() api
Now, upon re-reading, I wonder exactly what your asking. You must also make sure each
<div>must have a unique ID. You may consider you.next('div')instead of.closest()Per the discussion in comments, I would use .next(‘div’) look at the modified jsfiddle from yesterdays question http://jsfiddle.net/ycpgD/
also, I would strongly recommend using unique ID’s. if they cannot be unique make them classes.