Iterate through all but one elements in a form












0














I would love my while to iterate through all but one of the elements in the form. This is my code:



while (i < elnum && !empty) {
if (form.elements[i].value == "" && form.elements[i] != form.referral) {
error.innerHTML += 'All fields are required.</br>';
empty = true;
}
i++;
}


Where elnum is the number of elements.



Unfortunately, even if I leave only form.referral empty, it still enters inside the if. Basically, I want the check to be done for all fields but for that one.










share|improve this question




















  • 2




    something like if(badField){continue;} should work. Make sure it's the first statement in the if condition.
    – sissonb
    Apr 22 '13 at 21:24












  • What's form.referral?
    – Andrew Walters
    Apr 22 '13 at 21:27










  • Your code works just fine for me. jsfiddle.net/CzFWL
    – silly little me
    Apr 22 '13 at 21:34
















0














I would love my while to iterate through all but one of the elements in the form. This is my code:



while (i < elnum && !empty) {
if (form.elements[i].value == "" && form.elements[i] != form.referral) {
error.innerHTML += 'All fields are required.</br>';
empty = true;
}
i++;
}


Where elnum is the number of elements.



Unfortunately, even if I leave only form.referral empty, it still enters inside the if. Basically, I want the check to be done for all fields but for that one.










share|improve this question




















  • 2




    something like if(badField){continue;} should work. Make sure it's the first statement in the if condition.
    – sissonb
    Apr 22 '13 at 21:24












  • What's form.referral?
    – Andrew Walters
    Apr 22 '13 at 21:27










  • Your code works just fine for me. jsfiddle.net/CzFWL
    – silly little me
    Apr 22 '13 at 21:34














0












0








0







I would love my while to iterate through all but one of the elements in the form. This is my code:



while (i < elnum && !empty) {
if (form.elements[i].value == "" && form.elements[i] != form.referral) {
error.innerHTML += 'All fields are required.</br>';
empty = true;
}
i++;
}


Where elnum is the number of elements.



Unfortunately, even if I leave only form.referral empty, it still enters inside the if. Basically, I want the check to be done for all fields but for that one.










share|improve this question















I would love my while to iterate through all but one of the elements in the form. This is my code:



while (i < elnum && !empty) {
if (form.elements[i].value == "" && form.elements[i] != form.referral) {
error.innerHTML += 'All fields are required.</br>';
empty = true;
}
i++;
}


Where elnum is the number of elements.



Unfortunately, even if I leave only form.referral empty, it still enters inside the if. Basically, I want the check to be done for all fields but for that one.







javascript forms






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 5:49









Cœur

17.4k9102143




17.4k9102143










asked Apr 22 '13 at 21:22









Zagorax

6,90263348




6,90263348








  • 2




    something like if(badField){continue;} should work. Make sure it's the first statement in the if condition.
    – sissonb
    Apr 22 '13 at 21:24












  • What's form.referral?
    – Andrew Walters
    Apr 22 '13 at 21:27










  • Your code works just fine for me. jsfiddle.net/CzFWL
    – silly little me
    Apr 22 '13 at 21:34














  • 2




    something like if(badField){continue;} should work. Make sure it's the first statement in the if condition.
    – sissonb
    Apr 22 '13 at 21:24












  • What's form.referral?
    – Andrew Walters
    Apr 22 '13 at 21:27










  • Your code works just fine for me. jsfiddle.net/CzFWL
    – silly little me
    Apr 22 '13 at 21:34








2




2




something like if(badField){continue;} should work. Make sure it's the first statement in the if condition.
– sissonb
Apr 22 '13 at 21:24






something like if(badField){continue;} should work. Make sure it's the first statement in the if condition.
– sissonb
Apr 22 '13 at 21:24














What's form.referral?
– Andrew Walters
Apr 22 '13 at 21:27




What's form.referral?
– Andrew Walters
Apr 22 '13 at 21:27












Your code works just fine for me. jsfiddle.net/CzFWL
– silly little me
Apr 22 '13 at 21:34




Your code works just fine for me. jsfiddle.net/CzFWL
– silly little me
Apr 22 '13 at 21:34












1 Answer
1






active

oldest

votes


















2














Rather than trying to compare elements, try something like this:



if( form.elements[i].name == "referral") continue;


Put that just inside the loop, before the condition to check for an empty value.



That being said, it might be better to do something like this:



while(i < elnum) {
if( form.elements[i].hasAttribute("required") && form.elements[i].value == "") {
error.innerHTML += "All fields are required.<br />";
// re-add `empty=true` if the variable is needed elsewhere
// if it's only used to end the loop, then this is better:
break;
}
i++;
}


And make sure you add the required attribute to all required fields. This is a better solution because then it will take advantage of the browser's native ability to handle HTML5 forms, if it has any.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f16156973%2fiterate-through-all-but-one-elements-in-a-form%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Rather than trying to compare elements, try something like this:



    if( form.elements[i].name == "referral") continue;


    Put that just inside the loop, before the condition to check for an empty value.



    That being said, it might be better to do something like this:



    while(i < elnum) {
    if( form.elements[i].hasAttribute("required") && form.elements[i].value == "") {
    error.innerHTML += "All fields are required.<br />";
    // re-add `empty=true` if the variable is needed elsewhere
    // if it's only used to end the loop, then this is better:
    break;
    }
    i++;
    }


    And make sure you add the required attribute to all required fields. This is a better solution because then it will take advantage of the browser's native ability to handle HTML5 forms, if it has any.






    share|improve this answer


























      2














      Rather than trying to compare elements, try something like this:



      if( form.elements[i].name == "referral") continue;


      Put that just inside the loop, before the condition to check for an empty value.



      That being said, it might be better to do something like this:



      while(i < elnum) {
      if( form.elements[i].hasAttribute("required") && form.elements[i].value == "") {
      error.innerHTML += "All fields are required.<br />";
      // re-add `empty=true` if the variable is needed elsewhere
      // if it's only used to end the loop, then this is better:
      break;
      }
      i++;
      }


      And make sure you add the required attribute to all required fields. This is a better solution because then it will take advantage of the browser's native ability to handle HTML5 forms, if it has any.






      share|improve this answer
























        2












        2








        2






        Rather than trying to compare elements, try something like this:



        if( form.elements[i].name == "referral") continue;


        Put that just inside the loop, before the condition to check for an empty value.



        That being said, it might be better to do something like this:



        while(i < elnum) {
        if( form.elements[i].hasAttribute("required") && form.elements[i].value == "") {
        error.innerHTML += "All fields are required.<br />";
        // re-add `empty=true` if the variable is needed elsewhere
        // if it's only used to end the loop, then this is better:
        break;
        }
        i++;
        }


        And make sure you add the required attribute to all required fields. This is a better solution because then it will take advantage of the browser's native ability to handle HTML5 forms, if it has any.






        share|improve this answer












        Rather than trying to compare elements, try something like this:



        if( form.elements[i].name == "referral") continue;


        Put that just inside the loop, before the condition to check for an empty value.



        That being said, it might be better to do something like this:



        while(i < elnum) {
        if( form.elements[i].hasAttribute("required") && form.elements[i].value == "") {
        error.innerHTML += "All fields are required.<br />";
        // re-add `empty=true` if the variable is needed elsewhere
        // if it's only used to end the loop, then this is better:
        break;
        }
        i++;
        }


        And make sure you add the required attribute to all required fields. This is a better solution because then it will take advantage of the browser's native ability to handle HTML5 forms, if it has any.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Apr 22 '13 at 21:29









        Niet the Dark Absol

        256k53353466




        256k53353466






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f16156973%2fiterate-through-all-but-one-elements-in-a-form%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues