Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #19461 > unrolled thread

How do I use getElementById in my situation

Started by"Tony" <tony.johansson@inport.com>
First post2013-04-25 11:00 +0200
Last post2013-04-25 12:35 +0300
Articles 5 — 2 participants

Back to article view | Back to comp.lang.javascript


Contents

  How do I use getElementById in my situation "Tony" <tony.johansson@inport.com> - 2013-04-25 11:00 +0200
    Re: How do I use getElementById in my situation "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2013-04-25 12:19 +0300
      Re: How do I use getElementById in my situation "Tony" <tony.johansson@inport.com> - 2013-04-25 11:29 +0200
        Re: How do I use getElementById in my situation "Tony" <tony.johansson@inport.com> - 2013-04-25 11:32 +0200
        Re: How do I use getElementById in my situation "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2013-04-25 12:35 +0300

#19461 — How do I use getElementById in my situation

From"Tony" <tony.johansson@inport.com>
Date2013-04-25 11:00 +0200
SubjectHow do I use getElementById in my situation
Message-ID<klar57$cfi$1@dont-email.me>
I have a for loop from 0 to 8 and I want to fetch the DOM element id that is 
called elem0, elem1, elem2, elem3, elem4 , elem5, elem6, elem7, elem8 like 
this
for (var i = 0; i < antalColumner; i++) //0 - 7
{
  var item = "elem" + i;
   elem = document.getElementById(item);
}

I must make the getElementById to understand that I want "elem0" and "elem1" 
and so on

//Tony 

[toc] | [next] | [standalone]


#19462

From"Jukka K. Korpela" <jkorpela@cs.tut.fi>
Date2013-04-25 12:19 +0300
Message-ID<klas9o$idf$1@dont-email.me>
In reply to#19461
2013-04-25 12:00, Tony wrote:

> I have a for loop from 0 to 8 and I want to fetch the DOM element id
> that is called elem0, elem1, elem2, elem3, elem4 , elem5, elem6, elem7,
> elem8 like this
> for (var i = 0; i < antalColumner; i++) //0 - 7
> {
>   var item = "elem" + i;
>    elem = document.getElementById(item);
> }
>
> I must make the getElementById to understand that I want "elem0" and
> "elem1" and so on

So what is the problem? That seems to be what you are doing here. You 
are not using the value of the variable elem for anything, but that's a 
different issue.

On the other hand, if this is about processing a table, as the variable 
name antalColumner suggests, you could just traverse the table row by 
row and cell by cell, without needing id attributes.

-- 
Yucca, http://www.cs.tut.fi/~jkorpela/

[toc] | [prev] | [next] | [standalone]


#19463

From"Tony" <tony.johansson@inport.com>
Date2013-04-25 11:29 +0200
Message-ID<klassf$l35$1@dont-email.me>
In reply to#19462

"Jukka K. Korpela"  wrote in message news:klas9o$idf$1@dont-email.me... 

2013-04-25 12:00, Tony wrote:

> I have a for loop from 0 to 8 and I want to fetch the DOM element id
> that is called elem0, elem1, elem2, elem3, elem4 , elem5, elem6, elem7,
> elem8 like this
> for (var i = 0; i < antalColumner; i++) //0 - 7
> {
>   var item = "elem" + i;
>    elem = document.getElementById(item);
> }
>
> I must make the getElementById to understand that I want "elem0" and
> "elem1" and so on

So what is the problem? That seems to be what you are doing here. You 
are not using the value of the variable elem for anything, but that's a 
different issue.

On the other hand, if this is about processing a table, as the variable 
name antalColumner suggests, you could just traverse the table row by 
row and cell by cell, without needing id attributes.

-- 
Yucca, http://www.cs.tut.fi/~jkorpela/

When I do this
elem = document.getElementById(item);
elem is null

//Tony

[toc] | [prev] | [next] | [standalone]


#19464

From"Tony" <tony.johansson@inport.com>
Date2013-04-25 11:32 +0200
Message-ID<klat28$m32$1@dont-email.me>
In reply to#19463

"Tony"  wrote in message news:klassf$l35$1@dont-email.me... 



"Jukka K. Korpela"  wrote in message news:klas9o$idf$1@dont-email.me... 

2013-04-25 12:00, Tony wrote:

> I have a for loop from 0 to 8 and I want to fetch the DOM element id
> that is called elem0, elem1, elem2, elem3, elem4 , elem5, elem6, elem7,
> elem8 like this
> for (var i = 0; i < antalColumner; i++) //0 - 7
> {
>   var item = "elem" + i;
>    elem = document.getElementById(item);
> }
>
> I must make the getElementById to understand that I want "elem0" and
> "elem1" and so on

So what is the problem? That seems to be what you are doing here. You 
are not using the value of the variable elem for anything, but that's a 
different issue.

On the other hand, if this is about processing a table, as the variable 
name antalColumner suggests, you could just traverse the table row by 
row and cell by cell, without needing id attributes.

-- 
Yucca, http://www.cs.tut.fi/~jkorpela/

When I do this
elem = document.getElementById(item);
elem is null

//Tony

It works now

//Tony

[toc] | [prev] | [next] | [standalone]


#19465

From"Jukka K. Korpela" <jkorpela@cs.tut.fi>
Date2013-04-25 12:35 +0300
Message-ID<klat6f$mji$1@dont-email.me>
In reply to#19463
2013-04-25 12:29, Tony wrote:

> When I do this
> elem = document.getElementById(item);
> elem is null

Please teach your newsreader some habits; it now quoted my message 
comprensively, down to the signature, without marking quotations, and 
placed your text inside my sig!

Anyway, my crystal ball says that your script code is in your <head>, so 
it will be executed before the browser has read the <body>, so it does 
not know any id attribute values.

The simple (though not most elegant) fix is to place the <script> 
element at the very end of the <body>, i.e. right before the end tag 
</body>.

Well, it *could* be something else, too, like using id="Elem0" instead 
of id="elem0".

Please post a self-contained testable example or URL for such a thing, 
if further help is needed.

-- 
Yucca, http://www.cs.tut.fi/~jkorpela/

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web