473,396 Members | 1,810 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,396 software developers and data experts.

readystatechange, getElementByID help needed

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.onreadystatechange = handleResponse;
http.send(null);
}

function handleResponse() {
if(http.readyState == 4){
var replaceText = document.getElementById(name_textbox).value;
document.getElementById('name_displaybox_contents' ).innerHTML = replaceText;
document.getElementById('name_displaybox').style.d isplay = '';
document.getElementById('name_editingbox').style.d isplay = 'none';
}
}
------------

My thought was that at the <http.onreadystatechange = 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.readyState == 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 2389
epots9
1,351 Expert 1GB
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.onreadystatechange = handleResponse;
http.send(null);
}

function handleResponse() {
if(http.readyState == 4){
var replaceText = document.getElementById(name_textbox).value;
document.getElementById('name_displaybox_contents' ).innerHTML = replaceText;
document.getElementById('name_displaybox').style.d isplay = '';
document.getElementById('name_editingbox').style.d isplay = 'none';
}
}
------------

My thought was that at the <http.onreadystatechange = 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.readyState == 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
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.onreadystatechange = handleResponse(formelement);
http.send(null);
}

function handleResponse(divVariable) {
if(http.readyState == 4){
var replaceText = document.getElementById(divVariable).value;
document.getElementById(divVariable).innerHTML = replaceText;
}
}
-----------------------

the "if(http.readyState == 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 Expert 1GB
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.onreadystatechange = handleResponse(formelement);
http.send(null);
}

function handleResponse(divVariable) {
if(http.readyState == 4){
var replaceText = document.getElementById(divVariable).value;
document.getElementById(divVariable).innerHTML = replaceText;
}
}
-----------------------

the "if(http.readyState == 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 "formelement"? 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
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...
3
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...
15
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...
2
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,...
10
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...
10
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;...
11
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...
6
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
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...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...

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.