Computing.Net > Forums > Web Development > Javascript/DOM & Loops

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

Javascript/DOM & Loops

Reply to Message Icon

Name: mie2com
Date: August 3, 2007 at 14:59:35 Pacific
OS: XP
CPU/Ram: pentium 4
Product: dell
Comment:

Hi everyone. I need help.

Here's example of html & css;
[style]
.item_id {
visibility:hidden;
}
[/style]

[html]
...
[div id="ITEM_ID-A001" class="item_id"]...[/div]
[div id="ITEM_ID-A002" class="item_id"]...[/div]
...
[div id="ITEM_ID-A009" class="item_id"]...[/div]
...
[/html]

The problem is, if i wrote JS exactly like this:
document.getElementById("ITEM_ID-A001").style.visibility = "visible";
document.getElementById("ITEM_ID-A002").style.visibility = "visible";
...
document.getElementById("ITEM_ID-A009").style.visibility = "visible";
ALL [div] will become visible.

However, when i try to loop like this:
i = 0
while (i <= 9)
{
document.getElementById("ITEM_ID-A00" + i).style.visibility = "visible";
i++
}
ALL [div] still remain hidden.

If i have 999 items, it would take 999 lines to make it visible if i don't loop.
I need help on this. Thanks in advance.



Sponsored Link
Ads by Google

Response Number 1
Name: Michael J (by mjdamato)
Date: August 3, 2007 at 19:12:31 Pacific
Reply:

Well, that works for me just fine. Are you sure you don't have a typo or other problem with the code? By the way, I would suggest creating another class for the "visible" elements and changing the class of the divs. Just a thought.

Anyway, here is the test code I tried that worked:

<html>
<head>
<style>
.item_id {
visibility:hidden;
}
</style>

<script>

function runCode() {

document.getElementById("ITEM_ID-A001").style.visibility = "visible";

var i = 2;
while (i<=4) {
document.getElementById("ITEM_ID-A00"+i).style.visibility = "visible";
i++;
}
}

</script>

</head>

<body>
<div id="ITEM_ID-A001" class="item_id">1</div>
<div id="ITEM_ID-A002" class="item_id">2</div>
<div id="ITEM_ID-A003" class="item_id">3</div>
<div id="ITEM_ID-A004" class="item_id">4</div>

<button onclick="runCode()">Click</button>
</body>
</html>

Michael J


0

Response Number 2
Name: mie2com
Date: August 4, 2007 at 01:53:13 Pacific
Reply:

thanks MichaelJ. after studying your example, it works when I declare var i = 1. that's strange, i wonder why it didn't work when i declare var i = 0.


0

Response Number 3
Name: Michael J (by mjdamato)
Date: August 4, 2007 at 12:38:18 Pacific
Reply:

Well, that would because the first time through the loop it would be trying to reference the object with the ID 'A000'. And since that object does not exist you get the error. If you would have looked at the error message it should have told you that(double click the error icon in the bottom left of the browser window).

Michael J


0
Reply to Message Icon

Related Posts

See More


My Website.... what do yo... Password protect website ...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: Javascript/DOM & Loops

Javascript/DOM change row color www.computing.net/answers/webdevel/javascriptdom-change-row-color/3349.html

Javascript DOM Tutorial www.computing.net/answers/webdevel/javascript-dom-tutorial/2044.html

Hide the bottom frame with a button www.computing.net/answers/webdevel/hide-the-bottom-frame-with-a-button/3146.html