I have a javascript test function to determine if items have been entered but it appears that the function never completes, it runs the for loop but not the items after it??? I can produce a screen capture video of the events that happen, contact me and I will send a link to my download area. All of the code outside of the for loop (the last 5 lines) does not get executed???
here is the function that is triggered by a mouseover:

function testChecked(form) {
	var noneselected = 0;
	var errmsg = '';
	var iserror = 0;
	var formcount = document.forms.length;
alert('initial form length:' + formcount);
	for ( var cntr = 0; cntr <= formcount; cntr++ ) {
		if ( form['itemid'+(cntr+1)].checked ) {
			if ( form['selectterm'+(cntr+1)].value == 'select' )  {
				errmsg += 'You have checked but did not select a term for item: ' + (cntr+1) + '\n';
				iserror = 1;
				form['selectterm'+(cntr+1)].style.background = 'yellow';
			} else {
				errmsg += 'You have checked and selected: ' + (cntr+1) + form['selectterm'+(cntr+1)].value  + '\n';
				iserror = 1;
			}
		} else {
			if ( form['selectterm'+(cntr+1)].value != 'select' )  {
					errmsg += 'You have selected a term but did not check item: ' + (cntr+1) + '\n';
					form['selectterm'+(cntr+1)].style.background = 'yellow';
					iserror = 1;
				} else {
					errmsg += 'You have NOT checked and NOT selected: ' + (cntr+1) + form['selectterm'+(cntr+1)].value  + '\n';
				}
			}
alert('at end of loop form length:' + formcount);
alert('inside  cntrs:' + iserror + ' | ' + noneselected + '\n');
alert(errmsg);
	}
alert('outside cntrs:' + iserror + ' | ' + noneselected + '\n');
		alert ('nothing has been selected2');
	if ( noneselected == 1 ) {
		alert ('nothing has been selected1');
	}
	if ( iserror == 1 ) {
		alert (errmsg);
	}
}

Ebo,

Are you sure this is right?

function testChecked(form) {
	var formcount = document.forms.length;
	....
	for ( var cntr = 0; cntr <= formcount; cntr++ ) {
	....
}

Maybe my misunderstanding but the function appears to be checking a single form but then loops through all forms without addressing any form other than the one passed as a formal parameter. document.forms.length is the number of forms in the document, not the length of a particular form.

It seems possible that testChecked is terminating on error, hence non-execution of the last few lines. What does your error console tell you?

Airshow

OKAY MY BAD, I was reworking a script and it should be using document.form.length I changed it ALSO I should not be using form in the function [testChecked(form)] I have changed to to testChecked(formA)

But after making those changes it still fails, the value of form.length is 15 which is the right value, there are 2 checkboxes that I am checking. if checked then the corresponding select field must be selected for a term period, else an alert of if not checked and the term has been selected then an alert the the box wasn't checked.

But when I try to accumulate the err msgs to send only 1 alert it doesn't happen still fails to perform after the loop completes???

Thanks for the quick reply.

Ebo,

If you post your HTML then I can see if I can replicate your symptoms here.

(If you build the HTML server-side then post what you see in the browser with "View source")

Airshow

Ebo,

Hang on, maybe I've spotted it.

for ( var cntr = 0; cntr <= formcount; cntr++ ) {

This may give an error by trying to address form elements that don't exist.

Try:

for ( var cntr = 0; cntr < formcount; cntr++ ) {

Airshow

I have simplified the function shown below, now when I mouseover I will get the first alert which states 18
then I will get an alert for each checkbox I have setup (the form will vary, sometimes only 1 item others 3 or more)
I will get the 2nd alert msg for each checkbox that exists
IE: if I have 3 checkboxes I will get 3 alerts:
inside 1 | 18
inside 2 | 18
inside 3 | 18
I would think I should get 14 more alerts from "4 | 18" to "17 | 18"
I don't and I never get the last alert "outside" which is outside the for loop
should be straight forward, very confusing???

function testChecked(formA) {
    var noneselected = 0;
    var errmsg = '';
    var iserror = 0;
alert(formA.length);    
    for ( cntr = 1; cntr < formA.length; cntr++ ) {
        if ( formA['itemid'+cntr].checked ) {
            if ( formA['selectterm'+cntr].value == 'select' )  {
                alert ('You have checked but did not select a term for item: ' + cntr + '\n');
                formA['selectterm'+cntr].style.background = 'yellow';
            }
        } else {
            if ( formA['selectterm'+cntr].value != 'select' )  {
                    alert ('You have selected a term but did not check item: ' + cntr + '\n');
                    formA['selectterm'+cntr].style.background = 'yellow';
            }
        }
alert("inside " + cntr + " | " + formA.length);
    }
alert("outside");
}

I finally tracked the problem down, it was breaking out of the loop because when it looped past the actual number of checkbox's on the form it couldn't find it because it was undefined and therefor by Javascript standards it was an error.

By inserting a check for for undefined the loop works properly now.

If I have 3 checkbox's on the form the inside alert will display 3 times finally when it will continue trhu the loop for the next however many items exist and then exit properly and display the outside alert.

Many thanks for your help airshow maybe ill see you at Oshkosh sometime (unless that tag means something else.

function testChecked(formA) {
    var noneselected = 0;
    var errmsg = '';
    var iserror = 0;
    for ( cntr = 1; cntr < formA.length; cntr++ ) {
        [B]if ( formA['itemid' + (cntr)] == "undefined" ) {
[/B]
           if ( formA['itemid' + (cntr)].checked ) {
                if ( formA['selectterm ' + (cntr)].value == 'select' )  {
                    alert ('You have checked but did not select a term for item: ' + cntr + '\n');
                    formA['selectterm' + (cntr)].style.background = 'yellow';
                    iserror = 1;
                }
            } else {
                if ( formA['selectterm' + (cntr)].value != 'select' )  {
                    alert ('You have selected a term but did not check item: ' + cntr + '\n');
                    formA['selectterm' + (cntr)].style.background = 'yellow';
                    iserror = 1;
                }
            }
alert("inside " + cntr + " | " + formA.length); 
       [B] }[/B]
    }
alert("outside " + cntr + " | " + formA.length);
}

Ebo,

Well done. It had to be some sort of error. Nothing else made sense.

If you are interested, I'll send you my javascript Error Console, which is useful for debugging this sort of thing.

Not Oshkosh. I live very near the site of another world-famous airshow.

Airshow

thanks, I could use it route it to lbcdixon at gmail.com

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.