By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,686 Members | 1,840 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,686 IT Pros & Developers. It's quick & easy.

Recycling Ajax code for different elements

P: 160
Hi, I would like to pass different element id's to the stateChange() function from the add_to_list() function but i'm afraid i don't quite get it. The reason is that i would like to reuse the same ajax code for each new row added to a list.. there could be thousands of rows added with dynamically generated id's
so i want to pass that element_id along so I don't have to load an altered version of the code for each row. This is how i thought it would work but i'm missing something.. maybe i don't understand Objects..

Line 14 i'm trying to pass on the element_id as a variable to the stateChange() function, but it doesn't work.



Expand|Select|Wrap|Line Numbers
  1. function add_to_list(source, list, row, element_id)
  2.     { 
  3.     xmlHttp=GetXmlHttpObject()
  4.     if (xmlHttp==null)
  5.          {
  6.          alert ("Browser does not support HTTP Request")
  7.          return
  8.          }
  9.     var url="add_to_list.php"
  10.     url=url+"?source="+source
  11.     url=url+"&list="+list
  12.     url=url+"&row="+row
  13.     url=url+"&rand="+Math.random()
  14.     xmlHttp.onreadystatechange=stateChange(element_id)
  16.     xmlHttp.send(null)
  17.     }
  19. function stateChange(element_id)
  20.     { 
  21.     if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
  22.          { 
  23.          document.getElementById(element_id).innerHTML=xmlHttp.responseText 
  24.          } 
  25.     }
Jan 21 '09 #1
Share this Question
Share on Google+
2 Replies

Expert Mod 5K+
P: 8,639
line 14 is a callback, you can't use the brackets there, but you can set xmlHttp.requestid to hold the element_id (or use a closure....).
Jan 22 '09 #2

rnd me
Expert 100+
P: 427
it will probably work simply by changing
function stateChange(element_id)... to function stateChange()...
Jan 22 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.