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

getelementbyid problem

I am writing a webpage to display a small quiz, below is the code of the
webpage. I used a for loop so that if there are a lot of questions, it would
reduce the code. but the line ----
document.getElementById('id[i]').firstChild.data = 'CORRECT'; ---- doesn't
work with the ('id[i]') part. if i use ('id[0]') it works. the ans[i] works
too. is it something to do with calling the id from an array. how can i
solve this problem and keeping the for loop.

<HTML>
<HEAD>
<SCRIPT language = 'Javascript'>
var ans = new Array('0','0');
var correctans = new Array('4','2');
var correctansstring = new Array('4','6');
var id = new Array('id1','id2');

function check()
{
for(i = 0; i < 2; i++)
{
if(ans[i] == correctans[i])
{
document.getElementById('id[i]').firstChild.data = 'CORRECT';
document.getElementById('id[i]').style.color = 'green';
}
else
{
document.getElementById('id[i]').firstChild.data = 'WRONG\nThe correct
answer is: '+correctansstring[i];
document.getElementById('id[i]').style.color = 'red';
}
}
}
</SCRIPT>
</HEAD>
<BODY>
1. What is 2 + 2?<BR>
<INPUT type = 'radio' name = 'q1' onClick = 'ans[0] = 1'> 1<BR>
<INPUT type = 'radio' name = 'q1' onClick = 'ans[0] = 2'> 2<BR>
<INPUT type = 'radio' name = 'q1' onClick = 'ans[0] = 3'> 3<BR>
<INPUT type = 'radio' name = 'q1' onClick = 'ans[0] = 4'> 4<BR>
<a id=id[0]>&nbsp;</a>
<BR>
1. What is 3 + 3?<BR>
<INPUT type = 'radio' name = 'q2' onClick = 'ans[1] = 1'> 5<BR>
<INPUT type = 'radio' name = 'q2' onClick = 'ans[1] = 2'> 6<BR>
<INPUT type = 'radio' name = 'q2' onClick = 'ans[1] = 3'> 7<BR>
<INPUT type = 'radio' name = 'q2' onClick = 'ans[1] = 4'> 8<BR>
<a id=id[0]>&nbsp;</a>
<BR>
<INPUT type = 'button' value = 'CHECK' onClick = 'check()'><BR>
</BODY>
</HTML>
Jul 23 '05 #1
1 1427
On 17/04/2005 23:26, Warren Thai wrote:

[snip]
[T]he line ----
document.getElementById('id[i]').firstChild.data = 'CORRECT';
---- doesn't work with the ('id[i]') part.
If course not. There is no element with the id, 'id[i]'. Remove the
quotes so that the sequence is evaluated as an identifier and a
property accessor, not a string literal.

[snip]
<HEAD>
<SCRIPT language = 'Javascript'>
The language attribute is deprecated. Use the (required) type
attribute instead:

<script type="text/javascript">

You also seem to have omitted the required TITLE element.
var ans = new Array('0','0');
It tends to be better to use array literals, rather than Array
constructors:

var ans = ['0', '0'];

[snip]
<a id=id[0]>&nbsp;</a>


Out of interest, why are you writing into A elements? Wouldn't P or
SPAN elements be more appropriate? Secondly, square brackets are not
allowed as part of id attribute values. If you really want a
separator, you could use a hyphen (-), underscore (_), colon (:), or
dot (.).

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2

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

Similar topics

2
by: Gary Mayor | last post by:
Hi, I'm back again. Basically i'm trying to draw a box over an image which is turning out to be a nightmare. The problem i'm getting at the moment is that i'm creating a line with <div which works...
8
by: Gilles T. | last post by:
How I can get element ID in the edit mode of datagrid control? If I not in the edit mode, there are no problem. <asp:TemplateColumn ItemStyle-CssClass="grid_column_width_3"...
7
by: Gerry | last post by:
Hi, I have a javascript function which uses the method document.getElementById. I'm using it to decide whether a checkbox has been ticked or not. This decision is encoded in an if statement...
4
by: lawrence | last post by:
Can anyone tell me why this code works in Netscape 7.1 but not in IE??? <SCRIPT type='text/javascript'> function makeVisible(nameOfDiv) {...
10
by: JJA | last post by:
I'm trying to use document.getElementByID inside a function where the ID is passed as an argument. I get the same error ("Element has no properties") on the same statement inside the commonCheck...
24
by: Robi | last post by:
I have the following problem: I populate a page with a specific amount of <div id="MyTest"> containers inside another <div> container. for (i=0; i < MyString.length; i++) document.write('<div...
9
by: jason.hau | last post by:
Ok, interesting problem here, I have a webcontrol that holds a textbox and a requiredfieldvalidator from System.Web.UI.Webcontrols in .NET 2.0 (this is javascript related, bear with me). The...
4
by: Claudio Calboni | last post by:
Hello folks, I'm having some performance issues with the client-side part of my application. Basically, it renders a huge HTML table (about 20'000 cells in my testing scenario), without content....
3
by: djp1988 | last post by:
I have made up a code allowing users to click on a tab and show content in a neat white box, similar to what yahoo have on their homepage. I have used 5 divs, all but one set to display:none and...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.