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

readystatechange, getElementByID help needed

P: 2
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
Share this Question
Share on Google+
3 Replies


epots9
Expert 100+
P: 1,351
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

P: 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
Expert 100+
P: 1,351
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

Post your reply

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