[Javascript] If checkbox C_1 is selected fields required?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • viki1967
    Contributor
    • Oct 2007
    • 263

    #16
    Originally posted by acoder
    Don't loop over the elements array. Use fixed names/ids. That will allow you to get the correct elements. You could use the elements array/object if you provide names or set IDs and use document.getEle mentById().
    I have change method and function javascript and semplify the code... but if checkbox not selected alert is always KO!

    :(

    Code:
    function submitIt()
    
    { 
    
    
    
      var el = document.forms['myform'].elements;
      var s = "";
     
    
      var nFields = el.length;
     
      for ( var n = 0 ; n < nFields ; n++ )
      
        { 
          
          if (el[n].type == "checkbox" &&              
              el[n].checked);  {                       
                        
                 if(el[n].value.length <= 0)           
    
                       {
                       
                       alert("KO!");
                       el[n].focus();
                       return false;
                       
                       }
          
          else {
                                                              
            s+=el[n].value+',';
    
               }              
    
    }
    
    
            
    }
    
     alert("OK!");
       
    }
     
    ...
     
    <form id="id_form" action="" name="myform">
     
    ...
     
    <a href="#" onclick="return(submitIt(this));">GO!</a>

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #17
      The problem is still the same. After finding a checkbox and seeing that it's checked, you're checking the value of the same checkbox element (line 20). Also remove the semi-colon from line 18.

      Based on the HTML you posted earlier, I suggest you try something like the following:
      [code=javascript]var C1 = f.elements["C_1"];
      if (C1.checked) {
      var n1 = document.getEle mentById("n_1") ;
      // validate n1...
      var n2 = document.getEle mentById("n_2") ;
      // validate n2...
      }
      // now repeat for checkbox C_2...[/code]No need for a for loop to loop over all the form elements.

      Comment

      • viki1967
        Contributor
        • Oct 2007
        • 263

        #18
        Originally posted by acoder
        The problem is still the same. After finding a checkbox and seeing that it's checked, you're checking the value of the same checkbox element (line 20). Also remove the semi-colon from line 18.

        Based on the HTML you posted earlier, I suggest you try something like the following:
        [code=javascript]var C1 = f.elements["C_1"];
        if (C1.checked) {
        var n1 = document.getEle mentById("n_1") ;
        // validate n1...
        var n2 = document.getEle mentById("n_2") ;
        // validate n2...
        }
        // now repeat for checkbox C_2...[/code]No need for a for loop to loop over all the form elements.

        I need to loop because number of fields ( checkbox and other fields ) is variable in the form... do you understand my?

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #19
          Oh, I see. So then how would you determine which checkbox applies to which element?

          Comment

          • viki1967
            Contributor
            • Oct 2007
            • 263

            #20
            Originally posted by acoder
            Oh, I see. So then how would you determine which checkbox applies to which element?
            this is the problem...

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #21
              Let's try again. Is it always one checkbox followed by two text boxes?

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #22
                Originally posted by acoder
                Oh, I see. So then how would you determine which checkbox applies to which element?
                this is why I thought about using several forms. Is it a good idea to use: (foreach) form.submit() to send all forms?

                maybe you can add a class to every input group and access them through getElementsByCl assName()...

                regards

                Comment

                • viki1967
                  Contributor
                  • Oct 2007
                  • 263

                  #23
                  Originally posted by acoder
                  Let's try again. Is it always one checkbox followed by two text boxes?
                  Yes the form is always this: one checkbox followed by two text boxes.

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #24
                    Originally posted by viki1967
                    Yes the form is always this: one checkbox followed by two text boxes.
                    @ acoder: do you think node.nextSiblin g would be of use here?

                    Comment

                    • acoder
                      Recognized Expert MVP
                      • Nov 2006
                      • 16032

                      #25
                      Originally posted by viki1967
                      Yes the form is always this: one checkbox followed by two text boxes.
                      Since this is the case and there are no submit buttons, you can loop over the form elements array and add 3 each time instead of incrementing one only, e.g.
                      [code=javascript]for (n = 0; n < f.elements.leng th; n += 3) {
                      var chk = f.elements[n]; // checkbox
                      var txt1 = f.elements[n+1]; // text box 1
                      var txt2 = f.elements[n+2]; // text box 2
                      // now do the validation
                      [/code]

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #26
                        Originally posted by Dormilich
                        this is why I thought about using several forms. Is it a good idea to use: (foreach) form.submit() to send all forms?

                        maybe you can add a class to every input group and access them through getElementsByCl assName()...
                        That would be an idea, but rather than add it in the markup, I would say it would be better to check the type and check the next elements instead. I'm not too keen on the multiple forms idea because it causes more problems than it solves.

                        Comment

                        • acoder
                          Recognized Expert MVP
                          • Nov 2006
                          • 16032

                          #27
                          Originally posted by Dormilich
                          @ acoder: do you think node.nextSiblin g would be of use here?
                          Possibly or what I've posted above.

                          Comment

                          • Dormilich
                            Recognized Expert Expert
                            • Aug 2008
                            • 8694

                            #28
                            I'd go for your solution too (sounds more solid)

                            Comment

                            • viki1967
                              Contributor
                              • Oct 2007
                              • 263

                              #29
                              sorry... but I have reset all... I am desperate because do not find solution to the my problem javascript...

                              Comment

                              • acoder
                                Recognized Expert MVP
                                • Nov 2006
                                • 16032

                                #30
                                See #25 above. Did you try that?

                                Comment

                                Working...