if; else if logic

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

    if; else if logic

    I was under the impression that if you have a series of "if, else if, else if " clauses presented mutually exclusive alternatives. Apparently that is not the case. This code results in the var "issue" being copied into all three objects. How can I write it so the results are mutally exclusive?

    Code:
    var issue = "Property has been vacant more than 24 months";
    				if(document.getElementById('issue1').value==""){
    					document.getElementById('issue1').value = issue;
    				} else if(document.getElementById('issue2').value==""){
    					document.getElementById('issue2').value = issue;
    				} else if(document.getElementById('issue3').value==""){
    					document.getElementById('issue3').value = issue;
    				}
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    unless there is a loop or whatever around this code it is exclusive ... you might try the following similar example:

    [CODE=javascript]var foo = 'bar';

    var n1 = '';
    var n2 = '';
    var n3 = '';

    if (n1 == '') {
    n1 = foo;
    } else if (n2 == '') {
    n2 = foo;
    } else if (n3 == '') {
    n3 = foo;
    }

    alert(n1);
    alert(n2);
    alert(n3);[/CODE]
    btw. it would be better to use a switch(); statement for such series of decisions ...

    kind regards

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      The code should work. Show the context in which it's used: the HTML code, the function and where/how it's called.

      Comment

      • andersond
        New Member
        • Feb 2007
        • 110

        #4
        I would like the switch() suggestion; but, every example I found had the case resolve to a number. I don't think that will work.

        Here's what I'm trying to do. I have 31 questions a retail insurance agent must answer to get a quotation on an insurance policy submission. Any of the 31 can result in the submission being referred to underwriting, rather than being quoted. In order to speed up the process of determining what caused it to be referred I want to record the first 10, or fewer, problems (issue1 ...issue10). If the first problem occurs on question 27 I want the result to go into "issue1". And, if there are problems with #3, #14 and #30 I want those responses to go into issue1, issue2 and issue3. I want it to find the first empty field and fill it with the appropriate resonse -- and leave all the others blank. Hence my logic was to test if issue1="" and, if it does, fill it with the response for that question -- and so on until it found a blank field.

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5390

          #5
          in case you have a list and just want to fill the first empty, then a loop would be the best solution i think ... something like:

          Code:
          // ordered list of field ids
          var fieldIds = {
              issue1: 1, issue2: 1, issue3: 1
          };
          
          for (var i in fieldIds) {
              var field = document.getElementById(i);
          
              if (field.value == '') {
                  field.value = issue;
                  break;
              }
          }
          kind regards

          Comment

          Working...