473,508 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

AJAX innerHTML problem

The full code relating to this question can be found at
http://marc.info/?l=php-general&m=112198633625636&w=2

It describes a bare bones way of making an AJAX request.
It works fine for me except at the last step where data is put into an
innerHTML element within
<div id="foo">
</div>

Here is the function to do that:

function handleResponse() {
if(http.readyState == 4){
var response = http.responseText;
var update = new Array();
// var up0 = update[0];
// alert("up0 is " + up0);
if(response.indexOf('|' != -1)) {
update = response.split('|');
document.getElementById(update[0]).innerHTML = update[1];
}
}
}

I get an error stating that getElementById(update[0]) is either null
or not an object>

Using the commented out alert code I have confirmed that the update
array does in fact contain "foo" in update[0].

If I hardcode the value "foo" into the line thus
document.getElementById("foo").innerHTML = update[1];

update[1] is displayed ok within the foo div without error
I would be gratefulif somone could explain to me what is causing this
error

TIA
N
Mar 20 '07 #1
5 3824
noddy wrote:
The full code relating to this question can be found at
http://marc.info/?l=php-general&m=112198633625636&w=2

It describes a bare bones way of making an AJAX request.
It works fine for me except at the last step where data is put into an
innerHTML element within
<div id="foo">
</div>

Here is the function to do that:

function handleResponse() {
if(http.readyState == 4){
var response = http.responseText;
var update = new Array();
// var up0 = update[0];
// alert("up0 is " + up0);
if(response.indexOf('|' != -1)) {
update = response.split('|');
document.getElementById(update[0]).innerHTML = update[1];
}
}
}

I get an error stating that getElementById(update[0]) is either null
or not an object>

Using the commented out alert code I have confirmed that the update
array does in fact contain "foo" in update[0].

If I hardcode the value "foo" into the line thus
document.getElementById("foo").innerHTML = update[1];

update[1] is displayed ok within the foo div without error
I would be gratefulif somone could explain to me what is causing this
error
Hi,

This has nothing to do with AJAX.

Just alert (http.responseText);
to see what the response contains.
According to your code it should contain at least one |
I expect it doesn't.

an example:
var test = "one|two";
var myArr = test.split("|");
alert (myArr[0]);
alert (myArr[0]);
alert (myArr[2]);

The last alert will fail because the element is never filled by the split
return array since it contains only 2 elements.

I expect you have a similar problem in your responsetext.

In general: Always examine the response from an Ajax request.

Regards,
Erwin Moller
>
TIA
N
Mar 20 '07 #2
On Mar 20, 6:52 am, noddy <n...@toyland.comwrote:
The full code relating to this question can be found athttp://marc.info/?l=php-general&m=112198633625636&w=2

It describes a bare bones way of making an AJAX request.
It works fine for me except at the last step where data is put into an
innerHTML element within
<div id="foo">
</div>

Here is the function to do that:

function handleResponse() {
if(http.readyState == 4){
var response = http.responseText;
var update = new Array();
// var up0 = update[0];
// alert("up0 is " + up0);
if(response.indexOf('|' != -1)) {
Oops... should be if (response.indexOf('|') >= 0) {

update = response.split('|');
document.getElementById(update[0]).innerHTML = update[1];
You should confirm that the length of the array is also 2:

if (update.length >= 2) {
document.getElementById(update[0]).innerHTML = update[1];
}
}
}

}

I get an error stating that getElementById(update[0]) is either null
or not an object>

Using the commented out alert code I have confirmed that the update
array does in fact contain "foo" in update[0].

If I hardcode the value "foo" into the line thus
document.getElementById("foo").innerHTML = update[1];

update[1] is displayed ok within the foo div without error

I would be gratefulif somone could explain to me what is causing this
error

TIA
N

Mar 20 '07 #3
On Tue, 20 Mar 2007 12:51:52 +0100, Erwin Moller
<si******************************************@spam yourself.comwrote:
>Hi,

This has nothing to do with AJAX.
You are right - I am resending the query more appropriatelyhead
>Just alert (http.responseText);
to see what the response contains.
According to your code it should contain at least one |
I expect it doesn't.
Thank you but it does

N


Mar 21 '07 #4
On 20 Mar 2007 05:52:36 -0700, "Tom Cole" <tc****@gmail.comwrote:

> if(response.indexOf('|' != -1)) {

Oops... should be if (response.indexOf('|') >= 0) {
Really?
This line as it is seems to confirm the presence orabsence of | in
the string just fine
>
> update = response.split('|');
document.getElementById(update[0]).innerHTML = update[1];

You should confirm that the length of the array is also 2:
The length of the array is 2 .
Thanks for your response.

N
Mar 21 '07 #5
noddy said the following on 3/21/2007 12:00 AM:
On 20 Mar 2007 05:52:36 -0700, "Tom Cole" <tc****@gmail.comwrote:

>> if(response.indexOf('|' != -1)) {
Oops... should be if (response.indexOf('|') >= 0) {

Really?
This line as it is seems to confirm the presence orabsence of | in
the string just fine
No, it doesn't test the presence of |, it checks the presence of '|' !=
-1 You need to move the equality comparison outside the parentheses of
the indexOf search.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 21 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
1850
by: Joel Byrd | last post by:
I am making an AJAX call when any one of a number of inputs in a form (search criteria) are clicked, checked, changed, etc. When the user clicks, checks, whatever, I am trying to display a...
17
11837
by: Arjen | last post by:
Hi, I want to reload 2 divs at one click. Ive tried: <a href = "javascript:void(0);"...
4
2516
by: UKuser | last post by:
Hi, I'm working on the following code, which works fine in Firefox, but not in IE. The problem is its not posting the variable to my page and I'm thinking its something wrong with the...
0
7165
by: Tarik Monem | last post by:
I have been working on an all AJAX/DOM web site which is set to go live today and I thought I'd share my discoveries with all of you whom have helped me when I have encountered different issues along...
2
3130
by: shivendravikramsingh | last post by:
hi friends, i m using a ajax function for retrieving some values from a database table,and display the values in required field,my prob is that the ajax function i m using is working f9 once,but if...
3
3931
by: dhsieh | last post by:
I am trying out nested AJAX calls for the first time, but I seem to have hit a snag. The code snippet is the outer AJAX call and a function, it gathers information about a company. As we get towards...
7
3540
by: RaefKandeel | last post by:
Hi all, this is my first post. I have a little ajax assignment that works fine on mozilla firefox 2.0, but doesn't work at all on Internet Explorer 7. Apparently it has to do with the line where...
22
1666
by: sheldonlg | last post by:
I am looking for a clean solution to a problem that I solved in, what I call, a "dirty" way. Here is what I want to do. I have a dropdown list. Clicking on an item in the dropdown list invokes...
6
5384
by: rayliu | last post by:
Recently, I have been attempting to pick up AJAX and discovered a slight problem with innerHTML.. I have <div id='container'></div> then ..
0
7135
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
7342
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
7410
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
7505
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...
0
5650
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,...
0
3215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3201
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1570
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 ...
1
774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.