473,394 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Recycling Ajax code for different elements

bugboy
160 100+
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.

Thanks!

Bugboy

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)
  15.     xmlHttp.open("GET",url,true)
  16.     xmlHttp.send(null)
  17.     }
  18.  
  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.     }
  26.  
Jan 21 '09 #1
2 1335
Dormilich
8,658 Expert Mod 8TB
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
427 Expert 256MB
it will probably work simply by changing
function stateChange(element_id)... to function stateChange()...
Jan 22 '09 #3

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

Similar topics

31
by: Tony | last post by:
I just noticed that prototype.js is one of the files in the Ajax.NET distribution - I'm pretty concerned about this. Does anyone know if this is the same "prototype.js" that is not well-liked...
9
by: GTi | last post by:
I have a bizare Ajax POST problem. I use Ajax POST to send data to the server. The server get the POST and answer back. BUT if I POST the data once again I recieve the same answer that the first...
7
ak1dnar
by: ak1dnar | last post by:
Hi, I got this scripts from this URL There is Error when i submit the form. Line: 54 Error: 'document.getElementbyID(....)' is null or not an object What is this error. Complete Files
1
by: geevaa | last post by:
http://www.phpbuilder.com/columns/kassemi20050606.php3 XMLHttpRequest and AJAX for PHP programmers James Kassemi Introduction: Although the concept isn't entirely new, XMLHttpRequest...
4
by: Harshpandya | last post by:
HI all, I am working on the Ajax and PHP to make dynamic web page application I have HTML page - in which i am calling two different pages Google and NBC from Drop down menu. OUTPUT WANTED:...
2
by: rpollard | last post by:
Hi, I am relatively new to Ajax and am wondering if someone can shed a bit of light on the conceptual part of how Ajax handles its communication. Not knowing a whole lot about Ajax when I...
18
omerbutt
by: omerbutt | last post by:
AJAX PROB WITH MULTIPLE RECORDS helo iam having problem in ma code will any body look out an help, i am trying t add sale record in the database and the checkthe quantity of the part slod and...
4
by: Oriane | last post by:
Hello, I will soon installed a small Ajax Asp.Net "single page" site on a client site. This page is simply polling a Asp.Net Web service, which fetchs and returns parameters (temperature, air...
8
omerbutt
by: omerbutt | last post by:
hi there i have a form with multiple input (type/text ) fields and three inputs(type/file) fields i have to submit the form via ajax because i have multiple forms on this page ,you can say it is a...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.