Create functions dynamically

Create functions dynamically

I am reading from a database and read the number of elements from a text box at the end of the program (line 3). This is not working, but is there a way to achieve the same result? (assuming that there is no errors in my syntax).
Thank you in advance

  1. <script type="text/javascript">
  2.      $("document").ready(function(){

  3.        var total = $("#number_of_elements").val();
  4.        var name_header = "";
  5.        var name_div = "";
  6.        for(var i=1;i<=total;i=i+1){
  7.           name_header = ".top_header" + i;
  8.           name_div = ".top" + i;
  9.           alert(name_header + " " + name_div);
  10.             $(name_header).click(function(){
  11.             $(name_div).slideUp(200);
  12.          });
  13.        }
  14.     });
  15. </script