problem finding first blank field

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • andersond
    New Member
    • Feb 2007
    • 110

    problem finding first blank field

    I have a form with 31 questions each of which could cause an application to be rejected. I have 10 fields named issue 1...issue10. If a problem is found I want to save a message to the first empty issue field. The code is below. The problem is it uses two issue fields each time. For example if there is a problem with question 2 it puts the issue message in issue1 AND issue2. How can I get it to only use the first available field?

    Code:
    var issue = "Property is located within 10 miles of an ocean, bay, intercostal waterway";
        					if(document.getElementById('issue1').value==null || document.getElementById('issue1').value==""){
                				document.getElementById('issue1').value = issue;
                			} else if(document.getElementById('issue2').value==null || document.getElementById('issue2').value==""){
                				document.getElementById('issue2').value = issue;
                			} else if(document.getElementById('issue3').value==null || document.getElementById('issue3').value==""){
                				document.getElementById('issue3').value = issue;
                			} else if(document.getElementById('issue4').value==null || document.getElementById('issue4').value==""){
                				document.getElementById('issue4').value = issue;
                			} else if(document.getElementById('issue5').value==null || document.getElementById('issue5').value==""){
                				document.getElementById('issue5').value = issue;
                			} else if(document.getElementById('issue6').value==null || document.getElementById('issue6').value==""){
                				document.getElementById('issue6').value = issue;
                			} else if(document.getElementById('issue7').value==null || document.getElementById('issue7').value==""){
                				document.getElementById('issue7').value = issue;
                			} else if(document.getElementById('issue8').value==null || document.getElementById('issue8').value==""){
                				document.getElementById('issue8').value = issue;
                			} else if(document.getElementById('issue9').value==null || document.getElementById('issue9').value==""){
                				document.getElementById('issue9').value = issue;
                			} else if(document.getElementById('issue10').value==null||document.getElementById('issue10').value==""){
                				document.getElementById('issue10').value = issue;
                			}
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    although this has nothing to do with your problem I'd like to advice that you use a simple function to test, instead of manually checking all values.
    Code:
    function isEmpty(issue)
    {
        if (!this.value) // fires if the value is null, "", 0, false, [] or {}
        {
             this.value = issue;
             return true;
        }
        return false;
    }
    
    // loop over all elements
    // you may also use a for-loop
    var i=1;
    while (var inp = document.getElementById("issue"+i))
    {
        if (isEmpty.call(inp, "your_issue_message_here"))
            break;
        i++;
    }
    /* something like that..... */

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Your code (though it could be improved as suggested) should work fine. Something else must be affecting it. Is there any other code which sets the issue field values?

      Comment

      • andersond
        New Member
        • Feb 2007
        • 110

        #4
        Okay. Here is what I am using. It does find the first blank field named 'issue'+i.

        However, it never works again after the first time around. I need for it make this determination and post whatever the 'cNarrative' string is to the next blank 'issue'+i each time it finds a problem. What do I need to change to make this happen?

        Code:
        function isEmpty(cObject){ 
            if (!this.value){  
                 this.value = cObject; 
                 return true; 
            } 
            return false; 
        }
        function saveIssue(cNarrative){
        	var i;
        	var inp=document.getElementById('issue'+i);
        	for (i=1;i<11;i++) {
        		if(isEmpty.call(inp,cNarrative)){
          			document.getElementById('issue'+i).value = cNarrative;
          			break;
          		}
          	}
        }
        Here is an example of how it is called from another js function

        Code:
        var issue = "Property has been vacant more than 24 months";
        				saveIssue(issue);

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          Code:
          function saveIssue(cNarrative)
          {
          	var inp, i=1;
          	while (inp = document.getElementById("issue"+i)) 
          	{
          		if (isEmpty.call(inp, cNarrative))
          			break;
          		i++;
          	}	
          }
          worked for me (FF 3.0.8, MacOS 10.5.6) without problems.

          regarding your code:

          line #13 is unnecessary (the message is already printed in isEmpty())

          you have to put "inp" in the for-loop (now you're testing "undefined" repeatedly) (that's why I wrote "while(inp) {}", it only executes, if the element is actually present)

          Comment

          • andersond
            New Member
            • Feb 2007
            • 110

            #6
            I copied and pasted your code into my document and ran a few quick tests. So far it does find the first blank field but it fills two fields each time. For example, if question 2 has an issue it puts the issue (cNarrative) in fields 1 and 2. Then, the next issue gets placed in fields 3 and 4. How can I get it to only use one field?

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              didn't happen when I tested it. could you give a link, so that I can see the problem myself?

              Comment

              • andersond
                New Member
                • Feb 2007
                • 110

                #8
                The values of all variables can be seen by going to the VERY BOTTOM of the screen. That will be hidden when this goes live.

                Comment

                • Dormilich
                  Recognized Expert Expert
                  • Aug 2008
                  • 8694

                  #9
                  I just see a page with some hidden fields and without any javascript....

                  [edit] and my FF crashed when I tried the application a second time (because it wasn't responding)

                  Comment

                  • andersond
                    New Member
                    • Feb 2007
                    • 110

                    #10
                    Sorry 'bout that. The link I sent you runs the application. So you would have to press the submit to go to the second page. Here's the link to the actual page (data save will not occur at the end).

                    Comment

                    • Dormilich
                      Recognized Expert Expert
                      • Aug 2008
                      • 8694

                      #11
                      how do I trigger the script execution (clicking on the "water risk" radio button does nothing, not even selecting)

                      Comment

                      • andersond
                        New Member
                        • Feb 2007
                        • 110

                        #12
                        Using the water risk item as an example, our insurance carrier wants us to refer any risk that is near a water way. So, if you answer "no" nothing happens. If you answer "yes" that will cause a referral and, hence, an issue. Issues are recorded in the 10 issue fields that can be seen by going to the very bottom of the page.

                        Comment

                        • Dormilich
                          Recognized Expert Expert
                          • Aug 2008
                          • 8694

                          #13
                          once I figure out how to enable the radio button, I'll see what it gives.

                          Comment

                          • andersond
                            New Member
                            • Feb 2007
                            • 110

                            #14
                            To enable the radio button respond to the first question and tab.

                            Comment

                            • Dormilich
                              Recognized Expert Expert
                              • Aug 2008
                              • 8694

                              #15
                              if I select, say, Kentucky and tab then I'm coming to "© 2009" and the radio butten keeps disabled

                              Comment

                              Working...