getelementbyid problem

M

Michael Winter

On 17/04/2005 23:26, Warren Thai wrote:

[snip]
[T]he line ----
document.getElementById('id').firstChild.data = 'CORRECT';
---- doesn't work with the ('id') part.


If course not. There is no element with the id, 'id'. 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
 
W

Warren Thai

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').firstChild.data = 'CORRECT'; ---- doesn't
work with the ('id') part. if i use ('id[0]') it works. the ans 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 == correctans)
{
document.getElementById('id').firstChild.data = 'CORRECT';
document.getElementById('id').style.color = 'green';
}
else
{
document.getElementById('id').firstChild.data = 'WRONG\nThe correct
answer is: '+correctansstring;
document.getElementById('id').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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top