473,326 Members | 2,168 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,326 software developers and data experts.

Why does this not work properly?

I have a set of span placeholders with id's ranging from 0 through n as:

<span id=msgR0></span>
<span id=msgR1><span>
..etc.

These are used to display a message using innerHTML. I clear the message
using: document.getElementById(msgR0).innerHTML = ' ', etc.

I built a simple loop to clear all fields:

for (var i=0; i<n+1; i++) {
value = 'msgR'+i;
//alert("i: "+i+" value: "+value);
document.getElementById(value).innerHTML = ' '
}

It fails and returns the error message: message:
Could not convert undefined or null to object" "
document.getElementById(value).innerHTML = " ";

If I insert the alert() statement before the innerHTML directive, the
function works. If I rem it out, the function fails.

Huh?
--
Ed Jay (remove 'M' to respond by email)
Oct 4 '06 #1
2 4609
"Ed Jay" <ed***@aes-intl.comwrote in message
news:u8********************************@4ax.com...
I have a set of span placeholders with id's ranging from 0 through n as:

<span id=msgR0></span>
<span id=msgR1><span>
.etc.

These are used to display a message using innerHTML. I clear the message
using: document.getElementById(msgR0).innerHTML = ' ', etc.

I built a simple loop to clear all fields:

for (var i=0; i<n+1; i++) {
value = 'msgR'+i;
//alert("i: "+i+" value: "+value);
document.getElementById(value).innerHTML = ' '
}

It fails and returns the error message: message:
Could not convert undefined or null to object" "
document.getElementById(value).innerHTML = " ";

If I insert the alert() statement before the innerHTML directive, the
function works. If I rem it out, the function fails.

Huh?
--
Where is "n" declared? Anyway, this works for me:

<html>
<head>
<title>span_clear.htm</title>
<script type="text/javascript">
function span_clear() {
var max = 9;
var val;
for (var i=0; i<max+1; i++) {
val = "msgR"+i;
document.getElementById(val).innerHTML = "";
}
}
</script>
</head>
<body>
<a href="javascript:span_clear()">span clear</a>
<hr>
<span id="msgR0">0</span>
<span id="msgR1">1</span>
<span id="msgR2">2</span>
<span id="msgR3">3</span>
<span id="msgR4">4</span>
<span id="msgR5">5</span>
<span id="msgR6">6</span>
<span id="msgR7">7</span>
<span id="msgR8">8</span>
<span id="msgR9">9</span>
</body>
</html>
Though I might change
val = "msgR"+i;
document.getElementById(val).innerHTML = "";
to just
document.getElementById("msgR"+i).innerHTML = "";
Oct 4 '06 #2
McKirahan scribed:
>"Ed Jay" <ed***@aes-intl.comwrote in message
news:u8********************************@4ax.com.. .
>I have a set of span placeholders with id's ranging from 0 through n as:

<span id=msgR0></span>
<span id=msgR1><span>
.etc.

These are used to display a message using innerHTML. I clear the message
using: document.getElementById(msgR0).innerHTML = ' ', etc.

I built a simple loop to clear all fields:

for (var i=0; i<n+1; i++) {
value = 'msgR'+i;
//alert("i: "+i+" value: "+value);
document.getElementById(value).innerHTML = ' '
}

It fails and returns the error message: message:
Could not convert undefined or null to object" "
document.getElementById(value).innerHTML = " ";

If I insert the alert() statement before the innerHTML directive, the
function works. If I rem it out, the function fails.

Huh?
--

Where is "n" declared? Anyway, this works for me:

<html>
<head>
<title>span_clear.htm</title>
<script type="text/javascript">
function span_clear() {
var max = 9;
var val;
for (var i=0; i<max+1; i++) {
val = "msgR"+i;
document.getElementById(val).innerHTML = "";
}
}
</script>
</head>
<body>
<a href="javascript:span_clear()">span clear</a>
<hr>
<span id="msgR0">0</span>
<span id="msgR1">1</span>
<span id="msgR2">2</span>
<span id="msgR3">3</span>
<span id="msgR4">4</span>
<span id="msgR5">5</span>
<span id="msgR6">6</span>
<span id="msgR7">7</span>
<span id="msgR8">8</span>
<span id="msgR9">9</span>
</body>
</html>
Though I might change
val = "msgR"+i;
document.getElementById(val).innerHTML = "";
to just
document.getElementById("msgR"+i).innerHTML = "";
Thanks. It appears my error was failing to define the variable before
executing the loop.

(My head still hurts from banging it against the wall!)
--
Ed Jay (remove 'M' to respond by email)
Oct 4 '06 #3

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

Similar topics

3
by: John Bowling | last post by:
I have a java (2.0) program with the following lines: String cmdArray1 = {"lp", "-d", "hp4m", "MyFile"}; System.out.println(Runtime.getRuntime().exec(cmdArray1)); It compliles properly, but...
8
by: Jaime Rios | last post by:
Hi, I created a COM AddIn for Word that performs the functions that it needs to, but I needed to add the ability for the toolbar created by the COM AddIn to remember it's last position and...
0
by: Ross Bennett | last post by:
Ahoy, Folks! I've been looking all over for this, but I just can't seem to shake any documentation out of the MSDN or from Google. I've reviewed every .NET article on developing Windows...
6
by: Brian Miller | last post by:
I've been constructing an ASP.Net application using the 1.1 framework, and have been using Web Matrix for development purposes. Now that my application is near completion, I wanted to see if I can...
1
by: Wendy Elizabeth | last post by:
Can you give me some suggestions of why the xml web service is not working? I have an xml web service that works in my visual studio. net 1.1 environment. I setup this project up for deployment...
14
by: Anoop | last post by:
Hi, I am new to this newsgroup and need help in the following questions. 1. I am workin' on a GUI application. Does C# provides Layout Managers the way Java does to design GUI? I know that it...
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
9
by: archana | last post by:
Hi all, I want to know about interval of timer. I am using timer in windows service.I head somewhere that when i set interval property of timer while setting interval, restart time of Pc is...
18
by: Stephan Beal | last post by:
Hi, all! Before i ask my question, i want to clarify that my question is not about the code i will show, but about what the C Standard says should happen. A week or so ago it occurred to me...
0
by: osin | last post by:
Hello everyone, I need to generate an XML file whose element would be an active link (triggering a PERL script). The only tool I've found which is capable of doing it is Xlink. The problem is...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.