/    Sign up×
Community /Pin to ProfileBookmark

Brain Fart going on

I am not sure why this is alluding me but here it is. I need to test for the existence of a form field using javascript.

I have tried
if (document.getElementById(fldName)!= ‘null’)
if (document.getElementById(fldName)!= ‘undefined’)
I am needing to cycle through a set of form fields – that may or may not exist and add up there totals and alert the user of having hit the maximum.

This is the actual function… ( any ideas)

function checkNutrition(){
var intTotal = 0
for(i=24;i<=53;i++)
{
var fldName = “act_ID” + i;
if (document.getElementById(fldName)!= ‘null’){
if (document.getElementById(fldName).checked){
intTotal += document.getElementById(fldName).value;
}
}
}
if (intTotal >10){
alert(‘You have selected more than 10 nutrition points – please revist.’);
return false;
}else{
return true;
}
}

Thanks so much!

Chris

Chris Luksha
Echo Web Services
Making Your Website Resound
603-831-0099
[url]http://www.echowebservices.com/[/url]

And did I mention I Love building Cartweaver Modules

Shop Amazon Today!
[url]http://tinyurl.com/ymh3tc[/url]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@toicontienAug 27.2007 — Actually, it seems like you need to create a group of checkboxes by naming them the same.
[code=html]<input type="checkbox" name="act_ID" value="1"> ...
<input type="checkbox" name="act_ID" value="1"> ...[/code]

This naturally creates an array of node references to all form fields containing the name "act_ID".
var form = document.getElementById('myForm');
for (var i = 0, end = form.elements['act_ID'].length; i &lt; end; i ++) {
alert(form.elements['act_ID'][i]);
}
Copy linkTweet thisAlerts:
@clukshaauthorAug 27.2007 — I do in fact have all the form fields defined that I need on the form. However - the form fields are created dynamically in Cold Fusion.

So there might be act_ID1 but not act_ID 2

This is why I am trying to test for the existence of the field.

I have learned that this should tell me if the field exists...


[CODE]if (document.getElementById(fldName)){}[/CODE]

however it never returns true.

The full code now looks like this..
[CODE]
[sample inputs listed here for posting sake]
<input type="checkbox" name="act_ID24" class="frmCheckBox" value="1" onclick="updateTotals('activityForm');">
<input type="checkbox" name="act_ID25" class="frmCheckBox" value="1" onclick="updateTotals('activityForm');">

function checkNutrition(){
var intTotal = 0
for(i=24;i<=53;i++)
{
var fldName = "act_ID" + i;
alert (document.getElementById(fldName)); //This ALWAYS RETURNS NULL
if (document.getElementById(fldName)){
if (document.getElementById(fldName).checked){
intTotal += document.getElementById(fldName).value;
}
}
}
if (intTotal >10){
alert('You have selected more than 10 nutrition points - please revist.');
return false;
}else{
return true;
}
}[/CODE]
Copy linkTweet thisAlerts:
@clukshaauthorAug 27.2007 — GOT IT!

Because the form was being created dynamically - I never thought to look at it - BUT I was not declaring the ID of the form - just the Name - DUH!
×

Success!

Help @cluksha 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 6.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: @nearjob,
tipped: article
amount: 1000 SATS,

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

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