/    Sign up×
Community /Pin to ProfileBookmark

Undefined Checkbox Value – For Loop

I’m trying to iterate through a group of “month” checkboxes and capture the value of each; however, when I do an alert, I keep getting an initial value of “undefined” (though other values show up in the alert.) Can someone tell me – is the problem in my for loop, or where the error might be? Many thanks-

[CODE]
function submitPage(OT)
{
var MonthSelected;

for (i=0; i<=12; i++)
{
if (document.getElementById (“cbMonth” + i).checked)
{
MonthSelected = MonthSelected + “,”+ i;
}
}

if (OT == “”)
{
document.form.submit();
}

else
{
var outputPage=”post.asp?OT=” + OT;

if (MonthSelected != “”)
{
outputPage += “&MonthSelected=” + “,” + MonthSelected;
}

window.open(outputPage, “winOutput”);
}

function checkAllMonths(checkname, flag)
{
var flag;
var CBoxCount = 13;
var IDS = ”;

for (i = 0; i<CBoxCount; i++)

{
IDS = checkname+i;
document.getElementById(IDS).checked = flag;
}
}

[/CODE]

[code=html]
<p><input name=”cbMonth0″ type=”checkbox” id=”cbMonth0″ value=”0″ onClick=”checkAllMonths(‘cbMonth’,true)”>All Months</p>

<p><input name=”cbMonth1″ type=”checkbox” id=”cbMonth1″ value=”1″>January</p>

<p><input name=”cbMonth2″ type=”checkbox” id=”cbMonth2″ value=”2″>February</p>

<p><input name=”cbMonth3″ type=”checkbox” id=”cbMonth3″ value=”3″>March</p>

<p><input name=”cbMonth4″ type=”checkbox” id=”cbMonth4″ value=”4″>April</p>

<p><input name=”cbMonth5″ type=”checkbox” id=”cbMonth5″ value=”5″>May</p>

<p><input name=”cbMonth6″ type=”checkbox” id=”cbMonth6″ value=”6″>June</p>

<p><input name=”cbMonth7″ type=”checkbox” id=”cbMonth7″ value=”7″>July</p>

<p><input name=”cbMonth8″ type=”checkbox” id=”cbMonth8″ value=”8″>August</p>

<p><input name=”cbMonth9″ type=”checkbox” id=”cbMonth9″ value=”9″>September</p>

<p><input name=”cbMonth10″ type=”checkbox” id=”cbMonth10″ value=”10″>October</p>

<p><input name=”cbMonth11″ type=”checkbox” id=”cbMonth11″ value=”11″>November</p>

<p><input name=”cbMonth12″ type=”checkbox” id=”cbMonth12″ value=”12″>December</p>

<input type=”button” class= “button” onClick=”submitPage(‘h’)” value=”HTML”>
[/code]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERDec 16.2008 — You have created a variable called 'MonthSelected'

but you have never initialized it to anything I can find, hence 'undefined'

While I'm not sure what you are trying to accomplish, maybe this will work for you:
<i>
</i>var MonthSelected = '';
for (i=0; i&lt;=12; i++) {
if (document.getElementById ("cbMonth" + i).checked) {
MonthSelected += (i+1).toString()+','; <br/>
}
}
Copy linkTweet thisAlerts:
@baldwingrandauthorDec 16.2008 — That worked perfectly. And very simple! Thanks!!
Copy linkTweet thisAlerts:
@JMRKERDec 16.2008 — You're most welcome.

Glad I was able to help.

Good Luck!

?
×

Success!

Help @baldwingrand 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 4.30,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...