Create functions dynamically
In my site, I am reading from a database, get the total number of elements (line 3) and display chunks of data in div's. But I need to add functions to these div's. The following code is not working, is there an alternate way of achieving this?
Thanks in advance
- <script type="text/javascript">
- $("document").ready(function(){
- var total = $("#number_of_elements").val();
- var name_header = "";
- var name_div = "";
- for(var i=1;i<=total;i=i+1){
- name_header = "#top_header" + i;
- name_div = "#top" + i;
- $(name_header).click(function(){
- $(name_div).toggle("slow");
- });
- }
- });
- </script>