473,785 Members | 2,878 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

readystatechang e, getElementByID help needed

2 New Member
Hello everyone,

Long time lurker, first time poster.

I'm trying to implement an AJAX inline editor and am having some trouble. I want to use this editing field several times on
the same page, and was hoping to make the JavaScript reusable, passing
it the name of the fields that need to change, but I'm having some
trouble. In particular, I'm having trouble here:

------------
function sndReq(action) {
http.open('get' , action);
http.onreadysta techange = handleResponse;
http.send(null) ;
}

function handleResponse( ) {
if(http.readySt ate == 4){
var replaceText = document.getEle mentById(name_t extbox).value;
document.getEle mentById('name_ displaybox_cont ents').innerHTM L = replaceText;
document.getEle mentById('name_ displaybox').st yle.display = '';
document.getEle mentById('name_ editingbox').st yle.display = 'none';
}
}
------------

My thought was that at the <http.onreadyst atechange = handleResponse; >
line I could pass the value of the field element I'm using on to the
handleResponse function and then use it to swap out the
getElementByIds so that I wouldn't need to replicate all this code for
the different chunks I'm swapping out. However, the http.readyState
is making things difficult for me; for some reason, if I pass a
variable on to handleResponse, the "if (http.readyStat e == 4)" doesn't get
tripped.

Does this make any sense? I don't know, because it doesn't make sense to me either. If you could help me out I'd be greatly in your debt!

Mike
Jun 29 '07 #1
3 2411
epots9
1,351 Recognized Expert Top Contributor
Hello everyone,

Long time lurker, first time poster.

I'm trying to implement an AJAX inline editor and am having some trouble. I want to use this editing field several times on
the same page, and was hoping to make the JavaScript reusable, passing
it the name of the fields that need to change, but I'm having some
trouble. In particular, I'm having trouble here:

------------
function sndReq(action) {
http.open('get' , action);
http.onreadysta techange = handleResponse;
http.send(null) ;
}

function handleResponse( ) {
if(http.readySt ate == 4){
var replaceText = document.getEle mentById(name_t extbox).value;
document.getEle mentById('name_ displaybox_cont ents').innerHTM L = replaceText;
document.getEle mentById('name_ displaybox').st yle.display = '';
document.getEle mentById('name_ editingbox').st yle.display = 'none';
}
}
------------

My thought was that at the <http.onreadyst atechange = handleResponse; >
line I could pass the value of the field element I'm using on to the
handleResponse function and then use it to swap out the
getElementByIds so that I wouldn't need to replicate all this code for
the different chunks I'm swapping out. However, the http.readyState
is making things difficult for me; for some reason, if I pass a
variable on to handleResponse, the "if (http.readyStat e == 4)" doesn't get
tripped.

Does this make any sense? I don't know, because it doesn't make sense to me either. If you could help me out I'd be greatly in your debt!

Mike
Hi mikemcleod welcome to TSDN,
since it wasn't posted i think u forgot to do this:
Expand|Select|Wrap|Line Numbers
  1. var http;
  2. function sndReq(action) 
  3. {
  4. http = GetXmlHttpObject();
  5. http.open('get', action);
  6. http.onreadystatechange = handleResponse;
  7. http.send(null);
  8. }
  9. function GetXmlHttpObject()
  10. {
  11.     var xmlHttp=null;
  12.     try
  13.     {
  14.         // Firefox, Opera 8.0+, Safari
  15.         xmlHttp=new XMLHttpRequest();
  16.     }
  17.     catch (e)
  18.     {
  19.         // Internet Explorer
  20.         try
  21.         {
  22.             xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  23.         }
  24.         catch (e)
  25.         {
  26.             try
  27.             {
  28.                 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  29.             }
  30.             catch(e)
  31.             {
  32.                 xmlHttp=null;
  33.             }
  34.         }
  35.     }
  36.     return xmlHttp;
  37. }
  38.  
good luck
Jun 29 '07 #2
mikemcleod
2 New Member
Hi epots9,

Thanks for your message. I didn't post the part of my code where I open the XMLHttpRequest because I didn't think it was directly relevant to my problem.

The problem is that, when I modify the code I posted earlier to pass a variable on to the handleResponse function like this:

-----------------------
function sndReq(action, formelement) {
http.open('get' , action);
http.onreadysta techange = handleResponse( formelement);
http.send(null) ;
}

function handleResponse( divVariable) {
if(http.readySt ate == 4){
var replaceText = document.getEle mentById(divVar iable).value;
document.getEle mentById(divVar iable).innerHTM L = replaceText;
}
}
-----------------------

the "if(http.readyS tate == 4)" condition does not get tripped - when I don't pass the variable, as in the first script example I posted, the script works just fine, but I have to put the full name of the div into the getElementById function.

Totally baffled. Any help is much appreciated!

Mike
Jun 29 '07 #3
epots9
1,351 Recognized Expert Top Contributor
Hi epots9,

Thanks for your message. I didn't post the part of my code where I open the XMLHttpRequest because I didn't think it was directly relevant to my problem.

The problem is that, when I modify the code I posted earlier to pass a variable on to the handleResponse function like this:

-----------------------
function sndReq(action, formelement) {
http.open('get' , action);
http.onreadysta techange = handleResponse( formelement);
http.send(null) ;
}

function handleResponse( divVariable) {
if(http.readySt ate == 4){
var replaceText = document.getEle mentById(divVar iable).value;
document.getEle mentById(divVar iable).innerHTM L = replaceText;
}
}
-----------------------

the "if(http.readyS tate == 4)" condition does not get tripped - when I don't pass the variable, as in the first script example I posted, the script works just fine, but I have to put the full name of the div into the getElementById function.

Totally baffled. Any help is much appreciated!

Mike
what is the variable "formelemen t"? u're receiving it in the sndReq function, what is it? posts its html code and how your sedning it to the sndReq function.
Jun 29 '07 #4

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

Similar topics

2
9592
by: Greg | last post by:
I'm trying to understand getElementByID a bit better. When I try the following... if (parseFloat(document.getElementByID('QuantityMade').value) > 0) { alert('it seems to exist'); } (the text field QuantityMade holds a value of 17)
3
36185
by: sofie | last post by:
Hello all, I use the following javascript function in a html document to set the level of one of eight available radiobuttons. The line that's commented out works fine under IE, but I need to rewrite it so that it's W3C compliant. Reading through earlier messages I concluded I should go with getElementById but this doesn't work. What am I doing wrong? Your help is much appreciated.
15
3249
by: lawrence | last post by:
Sorry for the dumb question but I'm new to Javascript. I wrote this script hoping to animate some div blocks on a page. You can see the page here: http://www.keymedia.biz/demo.htm Can anyone tell me why these DIVs don't drift to the left as they are supposed to? <script language="javascript">
2
1412
by: SC | last post by:
I'm having a problem getting this validation script to work. There are two images on the page with the ids of img_antirobot and img_chk_agree. In the final page it will validate about 12 entries, so this is just a snippet. If there are blank entries, the script is to change the appropriate images to warning icons. Unfortunately it aways errors at the getElementById with "undefined" as the reason. I know the names are right, so what's the...
10
1909
by: Dave Hammond | last post by:
Hi All, The following code works in IE but not Firefox. IE produces the expected "this is more text!" output, but Firefox produces "no more text". Any ideas why? <BODY> <FORM> <INPUT TYPE=HIDDEN NAME="moreText" VALUE="<PRE>This is more text!</PRE>">
10
1423
by: Rich Hephner | last post by:
Why isn't this working? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <script language="javascript"> alert(document.getElementById('pst'));
11
2994
by: ctman770 | last post by:
Hi Everyone, Is it faster to save the precise location of an html dom node into a variable in js, or to use getElementById everytime you need to access the node? I want to make my application as fast as possible. I have about 10-20 id tags that need to be accessed and modified from time to time. Would the jvm perform slowly if I stored all of the dom node strings "document.node.child...." into a huge js array?
6
2116
by: Winston | last post by:
I want to changue the stylesheet for this page. Why the form select doesn't work? link id="est" rel="stylesheet" type="text/css" href="est.css"> function Changue() {
6
1834
by: Aaron Gray | last post by:
Hi, I know the 'document.getElementById()' issue is really over since every browser since 1998 supports the W3C DOM standard, but I could not resist this offering :- if (!document.getElementById) document.getElementById = function(id) { return document.all; } This should then provide a getElementById function if one does not already
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10329
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10152
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8974
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2880
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.