Connecting Tech Pros Worldwide Help | Site Map

Create a table dynamically

Newbie
 
Join Date: Oct 2007
Posts: 31
#1: Mar 13 '08
hi ,

I am developing a system using javascripts and ajax.
I am retrieving data from database and bind them in an html table. I am using a for loop to do this. This table has one column of html buttons. my task is to call a function when a user press the button in a row. this function will call an ajax method to bring some data fro the database. I have to pass a value to this function when a user press the button. this value is unique to each raw.

this is the part code i am trying;


Expand|Select|Wrap|Line Numbers
  1. // in the for loop
  2.  
  3.      btn=document.createElement('INPUT');
  4.          btn.type='button';
  5.           btn.value=Dset.value.Tables[0].Rows[a].CusCode;
  6.  
  7.          btn.onclick=function CustomerClick()
  8.               {
  9.                  // function will bring data                 
  10.                };
  11.  
  12. //
  13.  
this is the place i got the problem. I want to pass that 'CusCode' value to the function as avariable. this 'CusCode' is unique to each raw. when the user press the button it should call the function with it's unique variable.

can anyone help me to solve this?
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,127
#2: Mar 13 '08

re: Create a table dynamically


try this:

Expand|Select|Wrap|Line Numbers
  1. var btn = document.createElement('INPUT');
  2. btn.type = 'button';
  3. btn.value = Dset.value.Tables[0].Rows[a].CusCode;
  4.  
  5. btn.onclick = function(value) {
  6.     return function() {
  7.         // here use value which is the closured btn.value
  8.     };
  9. }(btn.value);
  10.  
kind regards
Newbie
 
Join Date: Oct 2007
Posts: 31
#3: Mar 14 '08

re: Create a table dynamically


wow, it works perfectly. Thank you very much.
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,127
#4: Mar 14 '08

re: Create a table dynamically


no problem :) .... post back to the forum anytime you have more questions ...

kind regards
Reply