/    Sign up×
Community /Pin to ProfileBookmark

Having some trouble just making a loop

I have a hidden element in HTML that contains a number of times I want a loop to run, based on the number of elements fetched from an SQL query.

I am doing the following code in JavaScript to make the loop run according the the value that is store in this hidden element (optCount).

The loop does not loop at all.

[CODE]var optCount = (document.getElementById(‘optionCount’).value);

// Add the options to the drop down menu
for(i=1; i < (optCount+1); i++)
{
}[/CODE]

What am I doing wrong?

EDIT: It seems to not loop at all, unless the number of loops is above 5, even with a manually entered number.

Thanks,
TK

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@AnotherMuggleauthorApr 07.2008 — Just realised it works OK in IE but not in Firefox.

Is there a known bug?
Copy linkTweet thisAlerts:
@Logic_AliApr 08.2008 — 
var optCount = (document.getElementById('optionCount').value); [/quote]
[I]optCount[/I] is a string. Try:
var optCount = Number(document.getElementById('optionCount').value);

if( !isNaN(optCount )
for(var i=1; i &lt; (optCount+1); i++)
{
...
}
Copy linkTweet thisAlerts:
@AnotherMuggleauthorApr 08.2008 — I actually have it working in Firefox now.

Was some code elsewhere that was interferring.

However, still got similar problems with Safari.

The Java Console is reporting that:
[CODE]document.getElementById('optionCount').value[/CODE]

is returning a NULL value.

Any ideas?
Copy linkTweet thisAlerts:
@Logic_AliApr 08.2008 — 
The Java Console is reporting that:
document.getElementById('optionCount').value
is returning a NULL value.[/quote]
You could be referencing it before it's rendered, but less unlikely if it works on other browsers. Try unhiding it and see what it displays. Otherwise need to see code.
Copy linkTweet thisAlerts:
@AnotherMuggleauthorApr 08.2008 — You could be referencing it before it's rendered, but less unlikely if it works on other browsers. Try unhiding it and see what it displays. Otherwise need to see code.[/QUOTE]

Thanks.

Fairly new to JavaScript.

How do I go about unhiding?

Thanks,

TK
×

Success!

Help @AnotherMuggle spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...