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

a getElementById question?

P: n/a
Hello,

Would appreciate a little help here!

How do I refer to multiple elements with IDs such as question1,
question2, question3 etc, in following?

document.getElementById('question').innerHTML

I want to use

for (var num=1;num<6;num++) {
document.getElementById('question').innerHTML
}

but cannot work out how to get the question1, question2, etc into the
code...

Thanks

Geoff
Oct 11 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
"Geoff Cox" <ge*******@notquitecorrectfreeuk.com> wrote in message
news:nv********************************@4ax.com...
Hello,

Would appreciate a little help here!

How do I refer to multiple elements with IDs such as question1,
question2, question3 etc, in following?

document.getElementById('question').innerHTML

I want to use

for (var num=1;num<6;num++) {
document.getElementById('question').innerHTML
}

but cannot work out how to get the question1, question2, etc into the
code...

Thanks

Geoff


Try

for (var num=1;num<6;num++) {
document.getElementById('question'+num).innerHTML
}
Oct 11 '05 #2

P: n/a
On Tue, 11 Oct 2005 15:04:33 -0500, "McKirahan" <Ne**@McKirahan.com>
wrote:
Try

for (var num=1;num<6;num++) {
document.getElementById('question'+num).innerHTML
}


Thanks for your reply - but I had tried the above - I get

document.getElementById(..] is null or not an object.

Any other thoughts?!

Cheers

Geoff


Oct 11 '05 #3

P: n/a
Geoff Cox wrote on 11 okt 2005 in comp.lang.javascript:
On Tue, 11 Oct 2005 15:04:33 -0500, "McKirahan" <Ne**@McKirahan.com>
wrote:
Try

for (var num=1;num<6;num++) {
document.getElementById('question'+num).innerHTML
}
Thanks for your reply - but I had tried the above - I get

document.getElementById(..] is null or not an object.


You would, if you forget to id them correctly

<div id='question1' ...
<div id='question2' ...
<div id='question3' ...
<div id='question4' ...
<div id='question5' ...
Any other thoughts?!


why?
--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Oct 11 '05 #4

P: n/a
Lee
Geoff Cox said:

Hello,

Would appreciate a little help here!

How do I refer to multiple elements with IDs such as question1,
question2, question3 etc, in following?

document.getElementById('question').innerHTML

I want to use

for (var num=1;num<6;num++) {
document.getElementById('question').innerHTML
}

but cannot work out how to get the question1, question2, etc into the
code...


This is extremely basic Javascript. PLEASE find a manual or tutorial.

"question"+num

Oct 11 '05 #5

P: n/a
On 11 Oct 2005 20:21:00 GMT, "Evertjan."
<ex**************@interxnl.net> wrote:
Geoff Cox wrote on 11 okt 2005 in comp.lang.javascript:
On Tue, 11 Oct 2005 15:04:33 -0500, "McKirahan" <Ne**@McKirahan.com>
wrote:
Try

for (var num=1;num<6;num++) {
document.getElementById('question'+num).innerHTML
}


Thanks for your reply - but I had tried the above - I get

document.getElementById(..] is null or not an object.


You would, if you forget to id them correctly

<div id='question1' ...
<div id='question2' ...
<div id='question3' ...
<div id='question4' ...
<div id='question5' ...


well - I have for example,

<td ID='question4' width='30%' valign='top' height='70'></td>

so that should be OK ?

Geoff
Oct 11 '05 #6

P: n/a
Geoff Cox wrote on 11 okt 2005 in comp.lang.javascript:
On 11 Oct 2005 20:21:00 GMT, "Evertjan."
<ex**************@interxnl.net> wrote:
Geoff Cox wrote on 11 okt 2005 in comp.lang.javascript:
On Tue, 11 Oct 2005 15:04:33 -0500, "McKirahan" <Ne**@McKirahan.com>
wrote:

Try

for (var num=1;num<6;num++) {
document.getElementById('question'+num).innerHTML
}

Thanks for your reply - but I had tried the above - I get

document.getElementById(..] is null or not an object.


You would, if you forget to id them correctly

<div id='question1' ...
<div id='question2' ...
<div id='question3' ...
<div id='question4' ...
<div id='question5' ...


well - I have for example,

<td ID='question4' width='30%' valign='top' height='70'></td>

so that should be OK ?


sure, but:

you will have tot test this for yourself in a simple setting,
and notin your seemingly productionready code, where you can make too
many mistakes.

first try my <div example for yourself

then start leaving out
width='30%' valign='top' height='70'
[which should be exchanged with css style,but that is another chapter]
or build a real simple test table

In general first make yourself at home with new [for you, seemingly]
concepts before applying them in a codewize real life situation.

Then your question "should this be OK" should not rize.
--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Oct 11 '05 #7

P: n/a
On 11 Oct 2005 20:21:00 GMT, "Evertjan."
<ex**************@interxnl.net> wrote:

have sorted it out - the problem was not with the

document.getElementById('question' + num).innerHTML

but with an index mismatch in that the element IDs started with
question1 (ie 1) but the related array for the text started with 0 and
I was using the same num variable for both....

apologies for wasting your time.

Cheers

Geoff
Oct 11 '05 #8

P: n/a
ASM
Geoff Cox a écrit :
On 11 Oct 2005 20:21:00 GMT, "Evertjan."
<ex**************@interxnl.net> wrote:

Geoff Cox wrote on 11 okt 2005 in comp.lang.javascript:

On Tue, 11 Oct 2005 15:04:33 -0500, "McKirahan" <Ne**@McKirahan.com>
wrote:
Try

for (var num=1;num<6;num++) {
document.getElementById('question'+num).innerHTML
}

Thanks for your reply - but I had tried the above - I get

document.getElementById(..] is null or not an object.


You would, if you forget to id them correctly

<div id='question1' ...
<div id='question2' ...
<div id='question3' ...
<div id='question4' ...
<div id='question5' ...

well - I have for example,

<td ID='question4' width='30%' valign='top' height='70'></td>

so that should be OK ?


no curiosity ? and you didn't try it ?
of course that would

but take care of what you write
it is not :
document.getElementById(..]
but
document.getElementById(..)
--
Stephane Moriaux et son [moins] vieux Mac
Oct 11 '05 #9

P: n/a
ASM
Geoff Cox a écrit :
with an index mismatch in that the element IDs started with
question1 (ie 1) but the related array for the text started with 0 and
I was using the same num variable for both....


it is a very curent mistake
all as growing to much the loop

--
Stephane Moriaux et son [moins] vieux Mac
Oct 11 '05 #10

P: n/a
On Wed, 12 Oct 2005 00:31:27 +0200, ASM
<st*********************@wanadoo.fr.invalid> wrote:
Geoff Cox a écrit :
with an index mismatch in that the element IDs started with
question1 (ie 1) but the related array for the text started with 0 and
I was using the same num variable for both....


it is a very curent mistake
all as growing to much the loop


Stephane,

How many times have I told myself to look for the simplest explanation
first !!!

Cheers

Geoff

Oct 12 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.