Iterate through all but one elements in a form
up vote
0
down vote
favorite
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
add a comment |
up vote
0
down vote
favorite
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
2
something likeif(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
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
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
javascript forms
edited Nov 12 at 5:49
Cœur
17.3k9102142
17.3k9102142
asked Apr 22 '13 at 21:22
Zagorax
6,88263348
6,88263348
2
something likeif(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
add a comment |
2
something likeif(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
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
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.
add a comment |
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',
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
up vote
2
down vote
accepted
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.
add a comment |
up vote
2
down vote
accepted
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.
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
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.
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.
answered Apr 22 '13 at 21:29
Niet the Dark Absol
255k53352465
255k53352465
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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