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

getElementById in a for loop whose target div id is being looped in that for loop.

Nik
Hello! Thanks for reading my question first!

I have many divs with an numeral id ,for example <div
id="999">...</div>, <div id="1000">...</div>

and I wish to do something like this,

for( var i = startvalue; i<=endvalue; i++){
document.getElementById(i).style.background="yello w;";
}

but after the first changing background, it stops and tell me that the
document.getElementById(i) has no properties. And I am sure that those
div's exist.

What's going wrong?

Thanks So Much!

Dec 15 '06 #1
10 10345
Nik said the following on 12/15/2006 12:08 AM:
Hello! Thanks for reading my question first!
What makes you think I read it "first" and not "second"?
I have many divs with an numeral id ,for example <div
id="999">...</div>, <div id="1000">...</div>

and I wish to do something like this,

for( var i = startvalue; i<=endvalue; i++){
document.getElementById(i).style.background="yello w;";
}

but after the first changing background, it stops and tell me that the
document.getElementById(i) has no properties. And I am sure that those
div's exist.

What's going wrong?
Something in your code isn't working? Show a sample page with a minimal
example of the behavior. Either your endvalue is wrong (depending on how
you set it) or the id doesn't exist.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 15 '06 #2
Nik
Hey Randy! Thanks for coming to rescue.

Unfortunately(really!) All the div's are pulled from a database with my
company's password.... BUT I have tried changing the
document.getElementById(i).style.background='yello w;'; to alert(i);

And that works - I pushed (endvalue - startvalue) times of OK myself.
But why doesn't the getElementById(i) work, this is very, very strange
to me, I am sensing something real stupid, but I cannot figure that out
myself...

any thoughts?

Quintillion Thank You's
Randy Webb wrote:
Nik said the following on 12/15/2006 12:08 AM:
Hello! Thanks for reading my question first!

What makes you think I read it "first" and not "second"?
I have many divs with an numeral id ,for example <div
id="999">...</div>, <div id="1000">...</div>

and I wish to do something like this,

for( var i = startvalue; i<=endvalue; i++){
document.getElementById(i).style.background="yello w;";
}

but after the first changing background, it stops and tell me that the
document.getElementById(i) has no properties. And I am sure that those
div's exist.

What's going wrong?

Something in your code isn't working? Show a sample page with a minimal
example of the behavior. Either your endvalue is wrong (depending on how
you set it) or the id doesn't exist.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 15 '06 #3
Nik wrote:
Hello! Thanks for reading my question first!

I have many divs with an numeral id ,for example <div
id="999">...</div>, <div id="1000">...</div>
For valid HTML, the value of the id attribute must not start with a
digit, though it can contain digits.
>
and I wish to do something like this,

for( var i = startvalue; i<=endvalue; i++){
document.getElementById(i).style.background="yello w;";
}

but after the first changing background, it stops and tell me that the
document.getElementById(i) has no properties. And I am sure that those
div's exist.

What's going wrong?
Who knows? Your code as posted should work. To fix the invalid ids,
just prefix the values with one or more letters, e.g.:

<script type="text/javascript">

function foo(startvalue, endvalue){
for( var i=startvalue; i<=endvalue; i++){
document.getElementById('x' + i).style.background="yellow;";
}
}

</script>
<input type="button" value="call foo(1,2)" onclick="foo(1,2);">
<div id="x0">x0</div>
<div id="x1">x1</div>
<div id="x2">x2</div>
<div id="x3">x3</div>
--
Rob

Dec 15 '06 #4
Nik
Hey Rob, Thank You, too, for your response!

I tried your suggestion, but it failed, too. Thank You nonetheless,
time and thought and everything. Just in case you still have the
slightest interest in this seemingly trivial problem, I tried in the
loop alert(i); instead of
document.getElementById(i).style.background='yello w;';, and that
worked! I had to press OK for.. many times (precisely
endvalue-startvalue times).

Any thoughts...

Thanks again!
RobG wrote:
Nik wrote:
Hello! Thanks for reading my question first!

I have many divs with an numeral id ,for example <div
id="999">...</div>, <div id="1000">...</div>

For valid HTML, the value of the id attribute must not start with a
digit, though it can contain digits.

and I wish to do something like this,

for( var i = startvalue; i<=endvalue; i++){
document.getElementById(i).style.background="yello w;";
}

but after the first changing background, it stops and tell me that the
document.getElementById(i) has no properties. And I am sure that those
div's exist.

What's going wrong?

Who knows? Your code as posted should work. To fix the invalid ids,
just prefix the values with one or more letters, e.g.:

<script type="text/javascript">

function foo(startvalue, endvalue){
for( var i=startvalue; i<=endvalue; i++){
document.getElementById('x' + i).style.background="yellow;";
}
}

</script>
<input type="button" value="call foo(1,2)" onclick="foo(1,2);">
<div id="x0">x0</div>
<div id="x1">x1</div>
<div id="x2">x2</div>
<div id="x3">x3</div>
--
Rob
Dec 15 '06 #5
Nik said the following on 12/15/2006 12:19 AM:
Hey Randy! Thanks for coming to rescue.
Answer:It destroys the order of the conversation
Question: Why?
Answer: Top-Posting.
Question: Whats the most annoying thing on Usenet?
Unfortunately(really!) All the div's are pulled from a database with my
company's password.... BUT I have tried changing the
document.getElementById(i).style.background='yello w;'; to alert(i);

And that works - I pushed (endvalue - startvalue) times of OK myself.
But why doesn't the getElementById(i) work, this is very, very strange
to me, I am sensing something real stupid, but I cannot figure that out
myself...
Do your div elements have a style and background property defined
already? IE doesn't like changing styles that are not explicitly declared.

<div id="a100" style="background:white">......</div>

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 15 '06 #6
Nik wrote:
Hey Rob, Thank You, too, for your response!

I tried your suggestion, but it failed, too. Thank You nonetheless,
time and thought and everything. Just in case you still have the
slightest interest in this seemingly trivial problem, I tried in the
loop alert(i); instead of
document.getElementById(i).style.background='yello w;';, and that
worked! I had to press OK for.. many times (precisely
endvalue-startvalue times).

Any thoughts...
Please post a complete HTML page example that is minimal (30 lines long
or less if possible) that displays the problem behavior. This will get
you the best concrete help as this is a concrete problem.

Peter

Dec 15 '06 #7
Nik
Hello Everyone who commented and care to come back to see how this
turned out:

I found the problem (a stupid one as suspected): Because not all my div
ids are continuous, meaning, instead of 1,2,3,4,5,6,... they are 1, 4
7, 11, 23, 53,55, and so on, so the loop stops whenever it finds that a
particular id of nonexistent. I put an if() on the
document.getelementbyId(i) to filter out the nonexistent ones and it
works wonderfully.

Thank you all again, really I almost scratched my eyes out to try to
figure this out.

Happy chanukah and merry xmas.
nik
Peter Michaux wrote:
Nik wrote:
Hey Rob, Thank You, too, for your response!

I tried your suggestion, but it failed, too. Thank You nonetheless,
time and thought and everything. Just in case you still have the
slightest interest in this seemingly trivial problem, I tried in the
loop alert(i); instead of
document.getElementById(i).style.background='yello w;';, and that
worked! I had to press OK for.. many times (precisely
endvalue-startvalue times).

Any thoughts...

Please post a complete HTML page example that is minimal (30 lines long
or less if possible) that displays the problem behavior. This will get
you the best concrete help as this is a concrete problem.

Peter
Dec 15 '06 #8
Nik wrote:
Hello! Thanks for reading my question first!

I have many divs with an numeral id ,for example <div
id="999">...</div>, <div id="1000">...</div>

and I wish to do something like this,

for( var i = startvalue; i<=endvalue; i++){
document.getElementById(i).style.background="yello w;";
}

but after the first changing background, it stops and tell me that the
document.getElementById(i) has no properties. And I am sure that those
div's exist.

What's going wrong?

Thanks So Much!
While your evaluation works you may end up doing a lot of unnecessary
looping. If your database is using an autoincrementing primary key, you
might get to a point where you're looping from 0 but none of the divs
begin until 10000 or something. You might want to use the
getElementsByTagName function to return only divs, so you're only
looping over tags that are important to you.

You can also get more granular control over groups of elements by
giving them a user defined attribute so you can tie them together, and
then using the getAttribute function to identify just the specific
elements that you have flagged for modification. Here's a code snippet
to get you started...

function changeReleventElements(selAttribute, assignVal){
// selAttribute can contain any style attribute to change, such as
"background"
// assignVal could be passed color, such as "yellow"
var aDivTags=document.getElementsByTagName("div") ?
document.getElementsByTagName("div") : document.all;
for (i=0; i<aDivTags.length; i++){
u=aDivTags[i].getAttribute("colorme");
if(u!==null){
eval("aDivTags[i].style."+selAttribute+"='"+assignVal+"'");
}
}
}

<div id="RandomNumber" colorme>
<input type="button" value="Change"
onClick="changeReleventElements('background','yell ow');">

Dec 16 '06 #9
pangea33 wrote:
Nik wrote:
>>Hello! Thanks for reading my question first!

I have many divs with an numeral id ,for example <div
id="999">...</div>, <div id="1000">...</div>

and I wish to do something like this,

for( var i = startvalue; i<=endvalue; i++){
document.getElementById(i).style.background="yel low;";
}

but after the first changing background, it stops and tell me that the
document.getElementById(i) has no properties. And I am sure that those
div's exist.

What's going wrong?

Thanks So Much!


While your evaluation works you may end up doing a lot of unnecessary
looping. If your database is using an autoincrementing primary key, you
might get to a point where you're looping from 0 but none of the divs
begin until 10000 or something. You might want to use the
getElementsByTagName function to return only divs, so you're only
looping over tags that are important to you.

You can also get more granular control over groups of elements by
giving them a user defined attribute so you can tie them together, and
then using the getAttribute function to identify just the specific
elements that you have flagged for modification. Here's a code snippet
to get you started...

function changeReleventElements(selAttribute, assignVal){
// selAttribute can contain any style attribute to change, such as
"background"
// assignVal could be passed color, such as "yellow"
var aDivTags=document.getElementsByTagName("div") ?
document.getElementsByTagName("div") : document.all;
for (i=0; i<aDivTags.length; i++){
u=aDivTags[i].getAttribute("colorme");
if(u!==null){

eval("aDivTags[i].style."+selAttribute+"='"+assignVal+"'");

Or simply: aDivTags[i].style[selAttribute]=assignVal;

Mick
}
}
}

<div id="RandomNumber" colorme>
<input type="button" value="Change"
onClick="changeReleventElements('background','yell ow');">
Dec 16 '06 #10

mick white wrote:
pangea33 wrote:
Nik wrote:
>Hello! Thanks for reading my question first!

I have many divs with an numeral id ,for example <div
id="999">...</div>, <div id="1000">...</div>

and I wish to do something like this,

for( var i = startvalue; i<=endvalue; i++){
document.getElementById(i).style.background="yell ow;";
}

but after the first changing background, it stops and tell me that the
document.getElementById(i) has no properties. And I am sure that those
div's exist.

What's going wrong?

Thanks So Much!

While your evaluation works you may end up doing a lot of unnecessary
looping. If your database is using an autoincrementing primary key, you
might get to a point where you're looping from 0 but none of the divs
begin until 10000 or something. You might want to use the
getElementsByTagName function to return only divs, so you're only
looping over tags that are important to you.

You can also get more granular control over groups of elements by
giving them a user defined attribute so you can tie them together, and
then using the getAttribute function to identify just the specific
elements that you have flagged for modification. Here's a code snippet
to get you started...

function changeReleventElements(selAttribute, assignVal){
// selAttribute can contain any style attribute to change, such as
"background"
// assignVal could be passed color, such as "yellow"
var aDivTags=document.getElementsByTagName("div") ?
document.getElementsByTagName("div") : document.all;
for (i=0; i<aDivTags.length; i++){
u=aDivTags[i].getAttribute("colorme");
if(u!==null){


eval("aDivTags[i].style."+selAttribute+"='"+assignVal+"'");

Or simply: aDivTags[i].style[selAttribute]=assignVal;

Mick
}
}
}

<div id="RandomNumber" colorme>
<input type="button" value="Change"
onClick="changeReleventElements('background','yell ow');">
Good call, Mick. I am working on the bad habit of using eval when it's
not needed.

Dec 16 '06 #11

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

Similar topics

6
by: Peter Ballard | last post by:
Whew. I hope that title is descriptive! Hi all, The python tutorial tells me "It is not safe to modify the sequence being iterated over in the loop". But what if my list elements are mutable,...
1
by: .Net Sports | last post by:
I am trying to get all the sales reps and their combined sales totals for a given queried date, in which i loop thru (using C# while loop) the available sales reps to get their rep IDs, then match...
3
by: Dr. Zharkov | last post by:
Hello. I converted the project from VB6 in VB .NET 2003, however in the following loop there are errors of a upgrade (in Help of VB .NET 2003 is not written, how to correct these errors): For x...
2
by: Jake Barnes | last post by:
Imagine I have these two lines of HTML: <p>Pick a list: (<a href="mcControlPanel.php" onClick="hideOrShowDivById('newMailList'); return false;">Create New List?</a>)</p> <form...
3
by: peter.meth | last post by:
Hi All, I am making a file manager type of application and am trying to duplicate Windows Explorer's behaviour when copying files; ie, display a second form with the copy files animation. As it...
2
by: alxasa | last post by:
Hi, I have a setInterval which executes its command every 10 seconds in a infinite loop. I've got something real basic like: var processes=0; function startme(){ if(stopthisloop>1)
1
by: clairelee0322 | last post by:
I am a c++ beginner and i am working on a dice loop lab. This program should roll two dice (each dice is from 1 to 6) and then total them together. The user should be able to input how many times...
1
by: jsmith | last post by:
Hi all, I need help on an elegant way of removing nodes from XML that do not have children with text that is kept in array. Here is the example: I have array of integers = {123, 456} and an...
4
by: naeem ayub | last post by:
Do While Not rss.EOF Do While Not prers.EOF If rss.Fields(0).Value = prers.Fields(1).Value Then ///match found some action.............. If Not rss.EOF Then rss.MoveNext prers.MoveFirst
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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...
0
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...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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

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.